I try to run rowMeans calculation inside the dplyr mutate function, but keep getting errors. The following is an example of a data set and the desired result.
DATA = data.frame(SITE = c("A","A","A","A","B","B","B","C","C"), DATE = c("1","1","2","2","3","3","3","4","4"), STUFF = c(1, 2, 30, 40, 100, 200, 300, 5000, 6000), STUFF2 = c(2, 4, 60, 80, 200, 400, 600, 10000, 12000)) RESULT = data.frame(SITE = c("A","A","A","A","B","B","B","C","C"), DATE = c("1","1","2","2","3","3","3","4","4"), STUFF = c(1, 2, 30, 40, 100, 200, 300, 5000, 6000), STUFF2 = c(2, 4, 60, 80, 200, 400, 600, 10000, 12000), NAYSA = c(1.5, 3, 45, 60, 150, 300, 450, 7500, 9000))
The code I wrote starts with random sampling of STUFF and STUFF2 . Then I would like to compute rowMeans from STUFF and STUFF2 and export the result to a new column. I could accomplish this task with tidyr , but I would have to redo a larger number of variables. In addition, I could use the basic R package, but prefer to find a solution using the mutate function in dplyr . Thanks in advance.
RESULT = group_by(DATA, SITE, DATE) %>% mutate(STUFF=sample(STUFF,replace= TRUE), STUFF2 = sample(STUFF2,replace= TRUE))%>% # These approaches return errors mutate(NAYSA = rowMeans(DATA[,-1:-2])) mutate(NAYSA = rowMeans(.[,-1:-2])) mutate (NAYSE = rowMeans(.))