简体   繁体   中英

Rearranging lists and dataframes in R

I have a list with a large number of data.frames (d=100) each containing a number of variables (v=10). I would like to rearrange the data so that I instead have a new list of 10 data.frames with 100 columns each, extracting the relevant column from each of the 100 original data.frames. So new data.frame1 will have 100 columns each being the 1st column from original data.frames, new data.frame2 will have 100 columns each being the 2nd column from original data.frames,....,n . What's the best way to rearrange this. Thank you in advance.

Here is a way, using dummy data, as you didn't provide input.

df1 <- df2 <- df3 <- df4 <- df5 <- data.frame(v1 = 1:3, v2 = 4:6)
lst <- mget(ls(pattern = "^df"))

I assume your list looks something like lst above. Here we have 5 data frames with 2 columns each and we rearrange it such that we end up with a list of 2 data frame containing 5 columns each.

split_idx <- seq_len(unique(lengths(lst))) # in your case, this should give you 1:10
out <- split.default(x = Reduce(cbind, lst), split_idx)
#$`1`
#  v1 v1.1 v1.2 v1.3 v1.4
#1  1    1    1    1    1
#2  2    2    2    2    2
#3  3    3    3    3    3

#$`2`
#  v2 v2.1 v2.2 v2.3 v2.4
#1  4    4    4    4    4
#2  5    5    5    5    5
#3  6    6    6    6    6

split.default splits a list along columns.


If we need to change the names of each data frame we might do

out <- lapply(out, function(x) {
  names(x) <- paste0(gsub("(v[0-9]+)\\.+", "\\1", names(x)), "_", 1:5)
  x
  })

out
#$`1`
#  v1_1 v1_2 v1_3 v1_4 v1_5
#1    1    1    1    1    1
#2    2    2    2    2    2
#3    3    3    3    3    3

#$`2`
#  v2_1 v2_2 v2_3 v2_4 v2_5
#1    4    4    4    4    4
#2    5    5    5    5    5
#3    6    6    6    6    6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM