我有以下列表
my_list <- list(df1 = data.frame(A = c(1:3), B1 = c(4:6), B2 = c(7:9), B3 = c(10:12)),
df2 = data.frame(A = c(1:4), B1 = c(5:8), B2 = c(9:12), B3 = c(13:16)),
df3 = data.frame(A = c(1:5), B1 = c(6:10), B2 = c(11:15), B3 = c(16:20)))
,我想将此列表中所有数据框中的列B1
,B2
和B3
重命名为new1
,new2
,和new3
。是否有一种方法可以使用基数R(也许使用grep
或gsub
函数)一步(或类似有效地)执行此操作?
答案 0 :(得分:4)
lapply(my_list, function(x) setNames(x, gsub("B", "new", names(x))))
#$`df1`
# A new1 new2 new3
#1 1 4 7 10
#2 2 5 8 11
#3 3 6 9 12
#$df2
# A new1 new2 new3
#1 1 5 9 13
#2 2 6 10 14
#3 3 7 11 15
#4 4 8 12 16
#$df3
# A new1 new2 new3
#1 1 6 11 16
#2 2 7 12 17
#3 3 8 13 18
#4 4 9 14 19
#5 5 10 15 20
答案 1 :(得分:1)
以编程方式实现此功能的简单函数,其重命名功能与dplyr::rename
或dplyr::select
相似(尽管不尽相同)
rename <- function(x, mapping) {
m <- match(mapping, colnames(x))
isna <- is.na(m)
colnames(x)[ m[!isna] ] <- names(mapping[!isna])
x
}
lapply(my_list, rename, c(new1="B1", new2="B2", new3="B3"))
# $df1
# A new1 new2 new3
# 1 1 4 7 10
# 2 2 5 8 11
# 3 3 6 9 12
# $df2
# A new1 new2 new3
# 1 1 5 9 13
# 2 2 6 10 14
# 3 3 7 11 15
# 4 4 8 12 16
# $df3
# A new1 new2 new3
# 1 1 6 11 16
# 2 2 7 12 17
# 3 3 8 13 18
# 4 4 9 14 19
# 5 5 10 15 20
它在没有匹配项时起作用:
rename(mtcars[1:2,], c(A="a"))
# mpg cyl disp hp drat wt qsec vs am gear carb
# Mazda RX4 21 6 160 110 3.9 2.620 16.46 0 1 4 4
# Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4