Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. table <- structure(list(species = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("Adelophryne adiastola",
  2. "Adelophryne gutturosa"), class = "factor"), scenario = structure(c(3L,
  3. 1L, 2L, 3L, 1L, 2L), .Label = c("future1", "future2", "present"
  4. ), class = "factor"), amount = c(5L, 3L, 2L, 50L, 60L, 40L)), .Names = c("species",
  5. "scenario", "amount"), class = "data.frame", row.names = c(NA,
  6. -6L))
  7. > table
  8. species scenario amount
  9. 1 Adelophryne adiastola present 5
  10. 2 Adelophryne adiastola future1 3
  11. 3 Adelophryne adiastola future2 2
  12. 4 Adelophryne gutturosa present 50
  13. 5 Adelophryne gutturosa future1 60
  14. 6 Adelophryne gutturosa future2 40
  15.  
  16. table %>%
  17. group_by(species) %>%
  18. mutate(tmp = amount[scenario == "present"]) %>%
  19. mutate(increase_amount = ifelse(amount > tmp, 1, 0))
  20. # Source: local data frame [6 x 5]
  21. # Groups: species [2]
  22. #
  23. # species scenario amount tmp increase_amount
  24. # <fctr> <fctr> <int> <int> <dbl>
  25. # 1 Adelophryne adiastola present 5 5 0
  26. # 2 Adelophryne adiastola future1 3 5 0
  27. # 3 Adelophryne adiastola future2 2 5 0
  28. # 4 Adelophryne gutturosa present 50 50 0
  29. # 5 Adelophryne gutturosa future1 60 50 1
  30. # 6 Adelophryne gutturosa future2 40 50 0
  31.  
  32. table$increase_amount <- with(table, as.integer(amount > ave(amount *
  33. (scenario == "present"), species, FUN = function(x) x[x!=0])))
  34. table$increase_amount
  35. #[1] 0 0 0 0 1 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement