重命名dataframe列列表以模拟连接的后缀

重命名dataframe列列表以模拟连接的后缀,r,list,purrr,R,List,Purrr,我有一个数据帧列表: dd <- list() dd$data <- list( ONE = data.frame(inAll = c(1.1,1.2,1.3), inAll_2 = c(1.4,1.5,1.6)), TWO = data.frame(inAll = c(2.1,2.2,2.3), inAll_2 = c(2.4,2.5,2.6)), THREE = data.frame(inAll = c(3.1,3.2,3.3), inAll_2 = c(3.4,3

我有一个数据帧列表:

dd <- list()

dd$data <- list(
  ONE = data.frame(inAll = c(1.1,1.2,1.3), inAll_2 = c(1.4,1.5,1.6)),
  TWO = data.frame(inAll = c(2.1,2.2,2.3), inAll_2 = c(2.4,2.5,2.6)),
  THREE = data.frame(inAll = c(3.1,3.2,3.3), inAll_2 = c(3.4,3.5,3.6)),
  FOUR = data.frame(inAll = c(4.1,4.2,4.3), inAll_2 = c(4.4,4.5,4.6)),
  FIVE = data.frame(inAll = c(5.1,5.2,5.3), inAll_2 = c(5.4,5.5,5.6)),
  SIX = data.frame(inAll = c(6.1,6.2,6.3), inAll_2 = c(6.4,6.5,6.6))
)
我期望的输出是

map(dd$data, list())
但我希望名称与精简数据集中的后缀相同

如何展开此映射函数,以便重命名列表中的列以反映缩减的名称

模式: [我试着查看了此连接的源代码!]看起来所有匹配但未连接的列都是:

  • 给定x然后y后缀
  • 这将继续与x_x和y_y等等
  • 如果重复列没有后缀的列表项数为最后一个
请注意,在我的示例中,这些数据帧通常有其他列,并且这些列的顺序并不总是相同的,因此我希望避免任何脆弱的情况,比如按索引匹配


new\u names我们可以用
strrep
(从
base R
)为'x','y'创建一个重复的字符串,
rep
复制它,用
map2
列表
列上循环,并通过
粘贴(
str\u c
)在
第二列重命名\u

library(dplyr)
library(purrr)
library(stringr)
n <- ceiling(length(dd$data)/2)
map2(dd$data,  strrep(rep(c('_x', '_y'), n), rep(seq_len(n), each = 2)), ~
             {nm <- .y
               .x %>% 
                 rename_at(vars(inAll_2), ~ str_c(., nm))
     })
#$ONE
#  inAll inAll_2_x
#1   1.1       1.4
#2   1.2       1.5
#3   1.3       1.6

#$TWO
#  inAll inAll_2_y
#1   2.1       2.4
#2   2.2       2.5
#3   2.3       2.6

#$THREE
#  inAll inAll_2_x_x
#1   3.1         3.4
#2   3.2         3.5
#3   3.3         3.6

#$FOUR
#  inAll inAll_2_y_y
#1   4.1         4.4
#2   4.2         4.5
#3   4.3         4.6

#$FIVE
#  inAll inAll_2_x_x_x
#1   5.1           5.4
#2   5.2           5.5
#3   5.3           5.6

#$SIX
#  inAll inAll_2_y_y_y
#1   6.1           6.4
#2   6.2           6.5
#3   6.3           6.6
库(dplyr)
图书馆(purrr)
图书馆(stringr)

n谢谢你的提问-我想重命名列,而不是减少它,我用reduce来说明我想如何使用相同的逻辑重命名列
new_names <- function(df) {
  # logic about new suffixes somehow
  map(df,list())
}
dd$data2 <- list(
  ONE = data.frame(inAll = c(1.1,1.2,1.3), inAll_2_x = c(1.4,1.5,1.6)),
  TWO = data.frame(inAll = c(2.1,2.2,2.3), inAll_2_y = c(2.4,2.5,2.6)),
  THREE = data.frame(inAll = c(3.1,3.2,3.3), inAll_2_x_x = c(3.4,3.5,3.6)),
  FOUR = data.frame(inAll = c(4.1,4.2,4.3), inAll_2_y_y = c(4.4,4.5,4.6)),
  FIVE = data.frame(inAll = c(5.1,5.2,5.3), inAll_2_x_x_x = c(5.4,5.5,5.6)),
  SIX = data.frame(inAll = c(6.1,6.2,6.3), inAll_2_y_y_y = c(6.4,6.5,6.6))
)

library(dplyr)
library(purrr)
library(stringr)
n <- ceiling(length(dd$data)/2)
map2(dd$data,  strrep(rep(c('_x', '_y'), n), rep(seq_len(n), each = 2)), ~
             {nm <- .y
               .x %>% 
                 rename_at(vars(inAll_2), ~ str_c(., nm))
     })
#$ONE
#  inAll inAll_2_x
#1   1.1       1.4
#2   1.2       1.5
#3   1.3       1.6

#$TWO
#  inAll inAll_2_y
#1   2.1       2.4
#2   2.2       2.5
#3   2.3       2.6

#$THREE
#  inAll inAll_2_x_x
#1   3.1         3.4
#2   3.2         3.5
#3   3.3         3.6

#$FOUR
#  inAll inAll_2_y_y
#1   4.1         4.4
#2   4.2         4.5
#3   4.3         4.6

#$FIVE
#  inAll inAll_2_x_x_x
#1   5.1           5.4
#2   5.2           5.5
#3   5.3           5.6

#$SIX
#  inAll inAll_2_y_y_y
#1   6.1           6.4
#2   6.2           6.5
#3   6.3           6.6