使用 rename_all 从列名中删除后缀? [英] Removing suffix from column names using rename_all?

查看:32
本文介绍了使用 rename_all 从列名中删除后缀?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个列的数据框,格式为 var1.mean、var2.mean.我想从包含它的所有列中删除后缀.mean".我尝试在管道中将 rename_all 与正则表达式结合使用,但无法提出正确的语法.有什么建议?

I have a data frame with a number of columns in a form var1.mean, var2.mean. I would like to strip the suffix ".mean" from all columns that contain it. I tried using rename_all in conjunction with regex in a pipe but could not come up with a correct syntax. Any suggestions?

推荐答案

如果您想使用 dplyr 包,我建议您使用 rename_at 函数.

If you want to use the dplyr package, I'd recommend using the rename_at function.

Dframe <- data.frame(var1.mean = rnorm(10),
                     var2.mean = rnorm(10),
                     var1.sd = runif(10))

library(dplyr)

Dframe %>% 
  rename_at(.vars = vars(ends_with(".mean")),
            .funs = funs(sub("[.]mean$", "", .)))

这篇关于使用 rename_all 从列名中删除后缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆