Reputation: 683
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)]
Upvotes: 18
Views: 26046
Reputation: 19191
Adding an option using colMeans
dto[, as.list(colMeans(.SD)), by=its]
its bananas melonas yeah
1: 1 2.0 7.0 12.0
2: 2 4.5 9.5 14.5
Choosing the columns by name
dto[, as.list(colMeans(.SD[, c("bananas", "melonas")])), by=its]
its bananas melonas
1: 1 2.0 7.0
2: 2 4.5 9.5
or by range
dto[, as.list(colMeans(.SD[, 2:3])), by=its]
its melonas yeah
1: 1 7.0 12.0
2: 2 9.5 14.5
Upvotes: 1
Reputation: 42582
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
Upvotes: 16
Reputation: 1114
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.
Upvotes: 19