use dplyr mutate to create new columns based on a vector of column names

前端 未结 3 1768
一整个雨季
一整个雨季 2021-01-20 19:31

I would like to take the log of some columns, and create new columns that are all named log[original column name].

The code below works, but how can I pass the vecto

3条回答
  •  春和景丽
    2021-01-20 20:03

    Ignoring the "use dplyr" part...

    require(data.table)    
    
    mtcars <- as.data.table(mtcars)
    
    mtcars[, paste0('log', columnstolog) := lapply(.SD, log), .SDcols = columnstolog]
    

提交回复
热议问题