Hence, all other columns are based on the same column in r
I have a large data frame that contains over 40,000 columns and I am facing a problem like this Sum over different column value in R
shop <- data.frame(
'shop_id' = c('Shop A', 'Shop A', 'Shop A', 'Shop B', 'Shop C', 'Shop C'),
'Assets' = c(2, 15, 7, 5, 8, 3),
'Liabilities' = c(5, 3, 8, 9, 12, 8),
'sale' = c(12, 5, 9, 15, 10, 18),
'profit' = c(3, 1, 3, 6, 5, 9))
I have a shop_id column that is repeated many times. I have other values โโassociated with this shop_id, such as assets, liabilities, profit, loss, etc. Now I want to average over all variables that have the same shop_id, that is, I want unique shop_ids and I want to average all columns that have the same shop_id. Because thousands of variables (columns) working with each column (variable) separately is very tedious.
My answer should be
shop_id Assets Liabilities sale profit
Shop A 8.0 5.333333 8.666667 2.333333
Shop B 5.0 9.000000 15.000000 6.000000
Shop C 5.5 10.000000 14.000000 7.000000
I currently use nested loops for the following: As generic as R, I believe there must be a faster way to do this
idx <- split(1:nrow(shop), shop$shop_id)
newdata <- data.frame()
for( i in 1:length(idx)){
newdata[i,1]<-c(names(idx)[i] )
for (j in 2:ncol(shop)){
newdata[i,j]<-mean(shop[unlist(idx[i]),j])
}
}
source to share
Try data.table
library(data.table)
setDT(shop)[, lapply(.SD, mean), shop_id]
# shop_id Assets Liabilities sale profit
#1: Shop A 8.0 5.333333 8.666667 2.333333
#2: Shop B 5.0 9.000000 15.000000 6.000000
#3: Shop C 5.5 10.000000 14.000000 7.000000
or
library(dplyr)
shop %>%
group_by(shop_id)%>%
summarise_each(funs(mean))
# shop_id Assets Liabilities sale profit
#1 Shop A 8.0 5.333333 8.666667 2.333333
#2 Shop B 5.0 9.000000 15.000000 6.000000
#3 Shop C 5.5 10.000000 14.000000 7.000000
or
aggregate(.~shop_id, shop, FUN=mean)
# shop_id Assets Liabilities sale profit
#1 Shop A 8.0 5.333333 8.666667 2.333333
#2 Shop B 5.0 9.000000 15.000000 6.000000
#3 Shop C 5.5 10.000000 14.000000 7.000000
For 40,000 columns, I would data.table
or might be dplyr
.
source to share
Use a ddply
function from the package plyr
:
> require("plyr")
> ddply(shop, ~shop_id, summarise, Assets=mean(Assets),
Liabilities=mean(Liabilities), sale=mean(sale), profit=mean(profit))
shop_id Assets Liabilities sale profit
1 Shop A 8.0 5.333333 8.666667 2.333333
2 Shop B 5.0 9.000000 15.000000 6.000000
3 Shop C 5.5 10.000000 14.000000 7.000000
source to share