R: Apply function on specific columns preserving the rest of the dataframe

心不动则不痛 提交于 2019-12-03 12:16:00

If you only want to do a computation on one or a few columns you can use transform or simply do index it manually:

# with transfrom:
df <- data.frame(A = 1:10, B = 1:10)
df <- transform(df, A = A*1000)

# Manually:
df <- data.frame(A = 1:10, B = 1:10)
df$A <- df$A * 1000

The following code will apply the desired function to the only the columns you specify. I'll create a simple data frame as a reproducible example.

(df <- data.frame(x = 1, y = 1:10, z=11:20))
(df <- cbind(df[1], apply(df[2:3],2, function(x){x*1000})))

Basically, use cbind() to select the columns you don't want the function to run on, then use apply() with desired functions on the target columns.

In dplyr we would use mutate_at in which you can select or exclude (by preceding variable name with "-" minus sign) specific variables. You can just name a function

df <- df %>% mutate_at(vars(columnA), scale)

or create your own

df <- df %>% mutate_at(vars(columnA, columnC), function(x) {do this})

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!