I have the following data frame:
id <- c(1,1,1,1,1,2,2,2,2)
spent <- c(10,10,20,10,10,5,5,5,20)
period <- c("f","c","c","v","v","f","c","c","v")
mean.spent <- c(10,15,15,10,10,5,5,5,20)
df <- data.frame(id,spent,period,mean.spent)
what I want is to aggregate the mean spent for each Id in each period as follow:
id f c v
1 10 15 10
2 5 5 20
Can you please help me to do this?
Use xtabs()
along with aggregate()
as follows:
df <- data.frame(id = c(1,1,1,1,1,2,2,2,2),
spent = c(10,10,20,10,10,5,5,5,20),
period = c("f","c","c","v","v","f","c","c","v"),
mean.spent = c(10,15,15,10,10,5,5,5,20))
xtabs(spent ~ id + period, aggregate(spent ~ id + period, df, mean))
# period
# id c f v
# 1 15 10 10
# 2 5 5 20
aggregate
calculates the mean per group (as grouped by "id" and "period"), and xtabs
does the transformation into this wider format.
Here's how to make it into a data.frame
:
temp1 <- xtabs(spent ~ id + period,
aggregate(spent ~ id + period, df, mean))
data.frame(id = dimnames(temp1)$id, as.data.frame.matrix(temp1))
# id c f v
# 1 1 15 10 10
# 2 2 5 5 20
I always forget about tapply
, but this example is a good candidate for when it is convenient.
tapply(df$spent, list(df$id, df$period), mean)
# c f v
# 1 15 10 10
# 2 5 5 20
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.