Within a dataframe I need to sum one column (val) by group to compare the result with some planned values (plan).
# sample data
df <- data.frame(ID = c(1:10), val = rep(10,10), group = c(1,2,3,2,4,5,5,5,6,7), plan =c(10,22,10,0,11,28,0,0,10,10))
One could calculate the new column "total_val" in several steps, e.g.
df2 <- aggregate(df$val, by=list(df$group), "sum")
df3 <- merge(df, df2, by.x = "group", by.y = "Group.1",all.x = TRUE)
df3$total_val <- ifelse(df3$plan == 0, 0, df3$x)
df3$x <- NULL
Note: in total_val the calculated group_sum must not be repeated, but appear ONLY in the first line of group like in the column "plan" (see marked lines with red).
But I am wondering whether someone knows a function or any other more efficient way to calculate a group_sum WITHIN an existing data frame?