Consider the following data frame:
df <- data.frame(a = c("01","01","02"),
b = c(101,101,101),
c = c(101,147,101),
d = c(100,200,500),
e = c(200,400,1000))
And:
agg <- data.frame(f = c("01","01","02","02"),
g = c("V1","V2","V1","V3"))
I want to change column a in df using agg. That is, if a = 01 then it should be changed to V1 and so on. However, in some cases some a in df goes into multiple names in g in agg. For instance, both 01 and 02 goes into V1. In that case, I want to calculate the average. However, I only want to calculate the average for d and e in df. That is, column b and c in df should be fixed. So I want following data frame:
a b c d e
1 V1 101 101 300 600
2 V2 101 101 100 200
3 V1 101 147 200 400
4 V2 101 147 200 400
5 V3 101 101 500 1000
As can be seen, column d and e in row 1 is an average.
How can I do that in R?
Aucun commentaire:
Enregistrer un commentaire