将前缀/后缀添加到列表中的多列 dfs

时间:2021-03-04 15:45:52

标签: r

我偶然发现了这个问题,如果有人可以帮助我,我将非常感激。另外,这是我第一次发帖,请耐心等待。

编辑:我提供的旧的可重现示例具有误导性,因为它表明列名称遵循 X1、X2、X3 等中的模式。我现在更新了示例。< /p>

我有一个包含 20 个数据框的列表。列数始终相同,但行数略有不同。我想通过向最后四列添加从 200020XX 的后缀或前缀来更改每个数据框的列名 - 也就是说除了第一列之外的每一列,这将起作用作为合并的关键。

下面我创建了一个可重现的示例:

a <- data.frame(items = sample(LETTERS,5,replace=FALSE),  
                sth = sample(0:10, 10,rep=TRUE),
                here = sample(0:10, 10,rep=TRUE),
                well = sample(0:10, 10,rep=TRUE))
b <- data.frame(items = sample(LETTERS,5,replace=FALSE),  
                sth = sample(0:10, 10,rep=TRUE),
                here = sample(0:10, 10,rep=TRUE),
                well = sample(0:10, 10,rep=TRUE))
c <- data.frame(items = sample(LETTERS,5,replace=FALSE),  
                sth = sample(0:10, 10,rep=TRUE),
                here = sample(0:10, 10,rep=TRUE),
                well = sample(0:10, 10,rep=TRUE))

list_raw <- list(a, b, c)

最终产品应如下所示:

A <- data.frame(items = sample(LETTERS, 5, replace = FALSE),
                sth_2000 = sample(0:10, 10,rep=TRUE),
                here_2000 = sample(0:10, 10,rep=TRUE),
                well_2000 = sample(0:10, 10,rep=TRUE))
B <- data.frame(items = sample(LETTERS, 5, replace = FALSE),
                sth_2001 = sample(0:10, 10,rep=TRUE),
                here_2001 = sample(0:10, 10,rep=TRUE),
                well_2001 = sample(0:10, 10,rep=TRUE))
C <- data.frame(items = sample(LETTERS, 5, replace = FALSE),
                sth_2002 = sample(0:10, 10,rep=TRUE),
                here_2002 = sample(0:10, 10,rep=TRUE),
                well_2002 = sample(0:10, 10,rep=TRUE))
list_not_so_raw_anymore <- list(A, B, C)

提前致谢!

3 个答案:

答案 0 :(得分:0)

base R中,我们可以遍历list,使用setNames通过pasteseq<创建新的列名来更改列名/p>

如果应该是相同的后缀,使用Map

Map(function(x, y) setNames(x, c("items", 
  paste0(names(x)[-1],  "_", rep(y, length(x) - 1)))),
     list_raw,  seq(2000,
         length.out = length(list_raw), by = 1))

-输出

[[1]]
   items sth_2000 here_2000 well_2000
1      C        5         1         4
2      J        2         3         7
3      Q        1         0         6
4      F        6         7         1
5      Y        2         6         6
6      C        6         1         2
7      J        4         2         2
8      Q       10         6         8
9      F        8         4         4
10     Y       10        10        10

[[2]]
   items sth_2001 here_2001 well_2001
1      G       10         9         6
2      H        7         8         6
3      J        6         1         2
4      S        9         7         7
5      A        0         6        10
6      G        1         5         3
7      H        7         2         6
8      J        3         0         9
9      S        4         6        10
10     A       10         7         8

[[3]]
   items sth_2002 here_2002 well_2002
1      Y        5         7         1
2      J        2         9         3
3      M        4         8         2
4      H       10         0         0
5      S       10         7         3
6      Y        5         7         3
7      J        7         4         2
8      M        4         2         8
9      H        9         7         6
10     S        8         1        10

答案 1 :(得分:0)

我想出了一些类似于 akrun 的代码。

a <- data.frame(items = sample(LETTERS,5,replace=FALSE),  
                replicate(5,sample(0:10, 10,rep=TRUE)))
b <- data.frame(items = sample(LETTERS,5,replace=FALSE),  
                replicate(5,sample(0:10, 15,rep=TRUE)))
c <- data.frame(items = sample(LETTERS,5,replace=FALSE),  
                replicate(5,sample(0:10, 20,rep=TRUE)))

list_raw <- list(a, b, c)

list_new <- lapply(seq_along(list_raw), function(i) {
                   y<-1999+i
                   setNames(list_raw[[i]], paste0(colnames(list_raw[[i]]), c('',paste0('_', rep(y, ncol(list_raw[[i]])-1)))))
                   })

list_new

答案 2 :(得分:0)

如果你知道你只想使用最后 4 列,你也可以硬编码你想使用的值并使用 lapply 和 library(data.table) 来解决重命名最后四列。

library(data.table)

vals <- c(2000:2003)
lapply(list_raw, 
function(f) setnames(f, 
            old = tail(names(f), 4), 
            new = paste0(vals, "_", tail(names(f), 4))))