rename list of dataframe columns to mimic joined suffixes

青春壹個敷衍的年華 提交于 2021-02-19 07:18:07

问题


I have a list of dataframes:

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))
)

And then reduce those dataframes using suffixes

reduce(dd$data, full_join, by = "inAll", suffix = c("_x", "_y"))

My desired output is

map(dd$data, list())

BUT I want the names to be the same as the suffixes in the reduced dataset.

How do I expand on this map function so that I rename the columns in the list to reflect the reduced names?

PATTERN:

[I tried looking at the join source code for this!] and it looks like all matching but not joined on columns are:

  • given _x then _y suffix
  • this continues with _x_x and _y_y and so on
  • if the number of list items with repeating column no suffix last

Note that these data frames in my example generally have other columns AND the columns are not always in the same order so I want to avoid anything fragile like matching by index!

new_names <- function(df) {
  # logic about new suffixes somehow
  map(df,list())
}

Desired Output

A list that looks like this:

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))
)


回答1:


We can create a repeated character string with strrep (from base R) for 'x', 'y', replicate it, loop over the list column with map2 and rename_at the 2nd column by pasteing (str_c) the suffix passed

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


来源:https://stackoverflow.com/questions/61374379/rename-list-of-dataframe-columns-to-mimic-joined-suffixes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!