Custom function to loop over a group in a dataframe.
Here is some sample data:
set.seed(42)
tm <- as.numeric(c("1", "2", "3", "3", "2", "1", "2", "3", "1", "1"))
d <- as.numeric(sample(0:2, size = 10, replace = TRUE))
t <- as.numeric(sample(0:2, size = 10, replace = TRUE))
h <- as.numeric(sample(0:2, size = 10, replace = TRUE))
df <- as.data.frame(cbind(tm, d, t, h))
df$p <- rowSums(df[2:4])
I created a custom function to calculate the value w:
calc <- function(x) {
data <- x
w <- (1.27*sum(data$d) + 1.62*sum(data$t) + 2.10*sum(data$h)) / sum(data$p)
w
}
When I run the function on the entire data set, I get the following answer:
calc(df)
[1]1.664474
Ideally, I want to return results that are grouped by tm, e.g.:
tm w
1 result of calc
2 result of calc
3 result of calc
So far I have tried using aggregate
with my function, but I get the following error:
aggregate(df, by = list(tm), FUN = calc)
Error in data$d : $ operator is invalid for atomic vectors
I feel like I have stared at this too long and there is an obvious answer.
dplyr
. Do you know what the equivalent would be? – Mediation