Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用rename_all从列名中删除数字_R_Dplyr_Rename - Fatal编程技术网

使用rename_all从列名中删除数字

使用rename_all从列名中删除数字,r,dplyr,rename,R,Dplyr,Rename,我正在尝试删除一些列名称中的数字。我试过以下方法,但运气不好 iris %>% rename_all(.funs = list(gsub('[[:digit:]]+', "", names(.)))) 如何使用rename\u all正确删除列名中的数字 数据: 数据(iris) rename\u中的numericNames直接传递所有列名 library(dplyr) iris %>% rename_all(~gsub('[[:digit:]]+',

我正在尝试删除一些列名称中的数字。我试过以下方法,但运气不好

iris %>% 
  rename_all(.funs = list(gsub('[[:digit:]]+', "", names(.))))
如何使用
rename\u all
正确删除列名中的数字

数据:

数据(iris)

rename\u中的numericNames直接传递所有
列名

library(dplyr)
iris %>%  rename_all(~gsub('[[:digit:]]+', "", .)) %>% head

#  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#1          5.1         3.5          1.4         0.2  setosa
#2          4.9         3.0          1.4         0.2  setosa
#3          4.7         3.2          1.3         0.2  setosa
#4          4.6         3.1          1.5         0.2  setosa
#5          5.0         3.6          1.4         0.2  setosa
#6          5.4         3.9          1.7         0.4  setosa

由于作用域变量
rename\u all
已从
dplyr
1.0.0版起被取代,因此应使用
rename\u和
如下:

iris %>% 
  rename_with(~gsub("\\d+", "", .))
或者,使用您使用的正则表达式

iris %>% 
  rename_with(~gsub('[[:digit:]]+', "", .))
引用列的名称,因此不需要使用
名称(.)

输出

#     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
# 1            5.1         3.5          1.4         0.2     setosa
# 2            4.9         3.0          1.4         0.2     setosa
# 3            4.7         3.2          1.3         0.2     setosa
# ...
#     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
# 1            5.1         3.5          1.4         0.2     setosa
# 2            4.9         3.0          1.4         0.2     setosa
# 3            4.7         3.2          1.3         0.2     setosa
# ...