r-在dplyr 1.0.2中,重命名以某个字符串开头的列的推荐方法是什么



我有一个tibble

xx <- structure(list(Abundance.126 = c(22.3, 24.5), Abundance.127N = c(23.4, 
22.7), Abundance.127C = c(16.6, 21.2), Abundance.128N = c(19.6, 
22.8), Abundance.128C = c(23.2, 25.9), Abundance.129N = c(22.7, 
25.6), Abundance.129C = c(20.7, 35.2), Abundance.130N = c(17, 
33.9), Abundance.130C = c(18.1, 25.8), Abundance.131N = c(20.3, 
28.9), Abundance.131C = c(18.8, 31.2), Abundance.132N = c(17.1, 
36.8), Abundance.132C = c(19, 41.5), Abundance.133N = c(19.2, 
35.3), Abundance.133C = c(21.7, 43.9), Abundance.134N = c(19.6, 
53.2)), row.names = 1:2, class = "data.frame")

我想重命名以字符串"开头的列;富足;(我还有很多其他专栏,但为了缩短帖子,我删除了它们(。我可以使用

rename_at(xx, vars(contains('Abundance.')), ~(sub('Abundance.', '', .)))

但是,读到vars((,我得到了:

https://dplyr.tidyverse.org/reference/vars.html

vars((只用于作用域动词,而作用域动词已被现有动词中的across((所取代。

我正确地理解vars((不应该再使用了吗?

rename_at/rename_if/rename_all已被rename_with取代。因此,在新的dplyr中,您可以将列重命名为:

library(dplyr)
xx %>% rename_with(~sub('Abundance.', '', .), contains('Abundance'))
#   126 127N 127C 128N 128C 129N 129C 130N 130C 131N 131C 132N 132C 133N 133C 134N
#1 22.3 23.4 16.6 19.6 23.2 22.7 20.7 17.0 18.1 20.3 18.8 17.1 19.0 19.2 21.7 19.6
#2 24.5 22.7 21.2 22.8 25.9 25.6 35.2 33.9 25.8 28.9 31.2 36.8 41.5 35.3 43.9 53.2

是的,不再需要vars()

最新更新