Reputation: 640
I am calculating a summary (mean) from a set of locations. And then need to re-assign these mean values to the locations from where these means were calculated.
What I did was to calculate those means and tried to create a new variable (average.yield) that assign the mean yield of the locations to each individual location.
cultivar <- c("cultivar_A", "cultivar_B")
data1 <- expand.grid(locs, cultivar)
yield <- c(7000, 5000, 2000, 7000, 6000, 5000)
data1$yield <- yield
colnames(data1) <- c("locs", "cultivar", "yield")
mean.yield.loc <- data1 %>%
group_by(locs) %>%
summarise(mean.yield = mean(yield))
data1 %>%
group_by(locs, cultivar) %>%
mutate(average.yield = mean.yield.loc$mean.yield)
I expected something like this..
locs cultivar yield average.yield
Loc_1 cultivar_A 7000 7000
Loc_2 cultivar_A 5000 5500
Loc_3 cultivar_A 2000 3500
Loc_1 cultivar_B 7000 7000
Loc_2 cultivar_B 6000 5500
Loc_3 cultivar_B 5000 3500
And this is the error:
Error: Column `average.yield` must be length 1 (the group size), not 3
Upvotes: 0
Views: 47
Reputation: 805
Just use the merge
function.
merge(data1,mean.yield.loc, by.x = "locs", by.y = "locs")
locs cultivar yield mean.yield
1 Loc_1 cultivar_A 7000 7000
2 Loc_1 cultivar_B 7000 7000
3 Loc_2 cultivar_A 5000 5500
4 Loc_2 cultivar_B 6000 5500
5 Loc_3 cultivar_A 2000 3500
6 Loc_3 cultivar_B 5000 3500
Upvotes: 1