使用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
# ...