简体   繁体   中英

Seeking fast or automated method for naming *many* new data.table columns in R

I have a large dataset, 3000x400. I need to created new columns that are means of the existing columns subsetted by a variable constituency . I have a list of new columns names that I want to use to name the new columns, below called newNames . But I can only figure out how to name columns when I directly type the desired new name.

What I currently do:

set.seed(1)
dataTest = data.table(turnout_avg = rnorm(20), urban_avg = rnorm(20,5,2), Constituency = c("A","B","C","D"), key = "Constituency")

oldColumnNames = c( "turnout_avg" , "urban_avg")

newNames = c( "turnout" ,   "urban")

# Here's my problem, naming these new columns
comm_means_by_district = cbind( 
dataTest[,list(Const_turnout = mean(na.omit(get(oldColumnNames[[1]])))), by= Constituency],
dataTest[,list(Const_urban = mean(na.omit(get(oldColumnNames[[2]])))),by= Constituency])

In reality, I want to create much more than two new columns. So I cannot feasibly type Const_turnout , Const_urban , etc. for all new columns.

I've have tried two ideas, but neither works, 1.

dataTest[,list(paste("district", newNames[1], sep="_") = mean(na.omit(get(refColNames[[1]])))), by= Constituency]

Or 2.

dataTest[,list(paste(oldColumnNames[1], "constMean", sep="_") = mean(na.omit(get(refColNames[[1]])))), by= Constituency]

first get the mean of all the columns in one go

DT <- dataTest[,lapply(.SD,function(x) mean(na.omit(x))), by= Constituency]

then change the colnames afterwards

setnames(DT,colnames(DT),vector_of_newnames)

Why is it important to change the names in the same line where you apply the function? I would just first calculate the constituency-wise means and set the column names after. Here's how this would look like:

dt <- dataTest[, lapply(oldColumnNames, function(x) mean(na.omit(get(x)))), 
               by=Constituency]
setnames(dt, c("Constituency", paste("Const", newNames, sep="_")))
dt

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM