I want to find the most efficient way to create a new variable. Suppose I have this data frame:
set.seed(1234)
df <- data.frame(group = c(rep(1,4), rep(2,4)), X = rep(1:4, 2), G = sample(1:10, 8, replace = T) )
I want to make a new variable that is the mean of G within each group, conditional on X being 1 or 2. In the example df, then, the new variable would have the following values:
df$newvar <- c(rep(4.5, 4), rep(8, 4))
Is there a way to do this without resorting the dataframe and then filling down? That seems really cumbersome. Thanks!