I have a mind numbing loop to write and your help would be greatly appreciated!
I am trying to summarise grouped data by excluding each row iteratively and summing the mean of the remaining group, then rejoining to form a new data frame.
So, for instance with this mockup data...
x <- c("A","A", "B", "B") y <- c(10:13) xy <- data.frame(x,y) %>% group_by(x) %>% mutate(index = row_number(x)) %>% ungroup() xy$uniq_id<- with(xy, paste0(index, x))
For 1A I the output value would be 11 because without row 1A, the sum of y for group A is 11. So, the value for 1B would be 13, and the value for 2B would be 12 ect.
Here is how I would do it manually for all the rows with index 1
xynew <- xy %>% group_by(x) %>% filter(index != "1") %>% mutate(sum_Sepal = sum(y)) full_join(xy, xynew, by = "uniq_id")
My data frame has 38 groups with 1-70 rows per group so I cant do this manually!
Your help in trying to writing a loop for this would be amazing.
Please let me know if you have any questions:)
Looking forward to hearing from you.