R从数据框中的变量名中删除后缀

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))