Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R data.table: mean for many columns

Tags:

r

data.table

I would like to use the data.table package in R to calculate column means for many columns by another set of columns. I know how to do this for a few columns, and I provide an example below. However, in my non-toy example, I have tens of variables I would like to do this for, and I would like to find a way to do this from a vector of the column names. Is this possible?

library(data.table)

# creates data table
dfo <- data.frame(bananas = 1:5, 
             melonas = 6:10,
             yeah = 11:15,
             its = c(1,1,1,2,2)
             )
dto <- data.table(dfo)

# gets column means by 'its' column
dto[,
.('bananas_mean' = mean(bananas),
  'melonas_mean' = mean(melonas),
  'yeah_mean' = mean(yeah)
  ),
by = .(its)]
like image 582
BioBroo Avatar asked May 05 '17 14:05

BioBroo


2 Answers

Using data.table:

library(data.table)
d <- dto[, lapply(.SD, mean), by=its]

d

   its bananas melonas yeah
1:   1     2.0     7.0 12.0
2:   2     4.5     9.5 14.5

Obviously, other functions could be used and combined. Hope it helps.

like image 170
COLO Avatar answered Sep 19 '22 13:09

COLO


The OP has requested to calculate column means for many columns ... from a vector of the column names. In addition, the OP has demonstrated in his sample code that he wants to rename the resulting columns.

Both the excepted answer and the solution suggested in this comment do not fully meet all these requirements. The accepted answer computes means for all columns of the data.table and doesn't rename the results. The solution in the comments does use a vector of column names and renames the results but modifies the original data.table while the OP expects a new object.

The requirements of the OP can be met using the code below:

# define columns to compute mean of
cols <- c("bananas", "melonas")
# compute means for selected columns and rename the output
result <- dto[, lapply(.SD, mean), .SDcols = cols, by = its
              ][, setnames(.SD, cols, paste(cols, "mean", sep = "_"))]

result
#   its bananas_mean melonas_mean
#1:   1          2.0          7.0
#2:   2          4.5          9.5

Means are only computed for columns given as character vector of column names, the output columns have been renamed, and dto is unchanged.

Edit Thanks to this comment and this answer, there is a way to make data.table rename the output columns automagically:

result <- dto[, sapply(.SD, function(x) list(mean = mean(x))), .SDcols = cols, by = its]
result
#   its bananas.mean melonas.mean
#1:   1          2.0          7.0
#2:   2          4.5          9.5
like image 33
Uwe Avatar answered Sep 20 '22 13:09

Uwe