R从数据框中的变量名中删除后缀 [英] R remove suffix from variable names in data frame
本文介绍了R从数据框中的变量名中删除后缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试删除 R 中数据框变量名称的后缀以聚合此列.
I'm trying to remove suffix to variable names of a data frame in R to aggregate this columns.
我已经将一个 Excel 表格导入到 R 中的数据框中,但是列名是这样导入的
I've imported a excel sheet into a data frame in R, but the column names where imported like this
var1...9 var2...10 var1...11 var2...12 var3.name...13
12 7 5 10 6
3 9 20 7 13
我需要的是删除最后一部分(从 ...)以按名称聚合列.
What I need is to remove the last part (from ...) to aggregate the columns by name.
var1 var2 var3.name
17 17 6
23 16 13
为此,我使用 dplyr
To do this I'm using dplyr
library(dplyr)
x %>%
rename_at(.vars = vars(ends_with("...*")),
.funs = funs(sub("[...]*$", "", .)))
但不起作用,我认为使用 * 不是使用通配符的合适方法...
but doesn't work, I think using * is not the appropriate way to use a wildcard...
推荐答案
我实际上认为在这里使用 base R 更容易:
I actually think using base R is easier here:
names(x) <- sub("\\.{3}\\d*$", "", names(x))
这篇关于R从数据框中的变量名中删除后缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文