R语言 向新列添加后缀

wrrgggsh  于 2023-03-10  发布在  其他
关注(0)|答案(1)|浏览(243)

旨在创建一个名为的新列,在应用函数“left”后包含后缀“_left”新DF列研究bd_acct bd_acct_left cc cc_left
与其手动更改名称,我更愿意找到一种自动添加后缀的方法

study <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
bd_acct <- c(57,66,72,24,76,98,56,32,43,250000,87,34,0,0,67)
cc <- c(1,2,3,5,74,3,5,6,5,3,5,6,7,4,1)
trial_A <- data.frame(study, bd_acct,cc) 

left <- function(x, na.rm = FALSE) (quantile(x,0.25) - 1.5*IQR(x))
trial_A %>% mutate_at(c("bd_acct", "cc"), left)
zpjtge22

zpjtge221#

dplyr::mutateacross().names参数一起使用:

library(dplyr)
trial_A %>% 
  mutate(across(bd_acct:cc, left, 
                .names = "{.col}_left"))

输出:

#    study bd_acct cc bd_acct_left cc_left
# 1      1      57  1        -28.5   -0.75
# 2      2      66  2        -28.5   -0.75
# 3      3      72  3        -28.5   -0.75
# .....

相关问题