1

assume, that I've a list of data frames and every data frame inside the list has the same structure:

$final01_1
    k    h      value
85 12 h=12 0.02867449
86 11 h=23 0.02885711
87 10 h=15 0.02868290
88  9 h=14 0.02872784
89  8 h=13 0.02864886
90  7 h=19 0.02869149
.   . ..    .....

$final02_1
    k    h      value
85 12 h=12 0.02013402
86 11 h=17 0.02012700
87 10 h=17 0.01972729
88  9 h=26 0.02025178
89  8 h=13 0.02035556
90  7 h=13 0.02022585

$final03_1
k    h      value
85 12 h=55 0.04254915
86 11 h=56 0.04283464
87 10 h=24 0.04244404
88  9 h=22 0.04239187
89  8 h=21 0.04318766
90  7 h=21 0.04510606

and so on. I want to create (in this case) 6 new data frames crated by the rows of each $final. That means the first data frame should contain all first rows of each data frame like this:

$foo
85 12 h=12 0.02867449
85 12 h=12 0.02013402
85 12 h=55 0.04254915

and this for all rows. I thinks something with lapply would be the solution, but I don't get an efficient solution.

Hope, that you can help me

1 Answer 1

2

We can loop through the rownames and then extract the rows

lst2 <- lapply(rownames(lst[[1]]), function(rn)
           do.call(rbind, lapply(lst, function(y) y[rn,])))

Note: A data.frame cannot have the same row names so, the duplicate row names are made into a unique row name with make.unique


This can also be done with split. Loop through the list with map_df, create a row names column with (rownames_to_column) and get a single dataset, then split by the 'rn' column so that we get a list of data.frames having the same 'rn'

library(tidyverse)
lst %>%
    map_df(rownames_to_column, 'rn') %>%
    split(.$rn)

-output

#$`85`
#   rn  k    h      value
#1  85 12 h=12 0.02867449
#7  85 12 h=12 0.02013402
#13 85 12 h=55 0.04254915

#$`86`
#   rn  k    h      value
#2  86 11 h=23 0.02885711
#8  86 11 h=17 0.02012700
#14 86 11 h=56 0.04283464

#$`87`
#   rn  k    h      value
#3  87 10 h=15 0.02868290
#9  87 10 h=17 0.01972729
#15 87 10 h=24 0.04244404

#$`88`
#   rn k    h      value
#4  88 9 h=14 0.02872784
#10 88 9 h=26 0.02025178
#16 88 9 h=22 0.04239187

#$`89`
#   rn k    h      value
#5  89 8 h=13 0.02864886
#11 89 8 h=13 0.02035556
#17 89 8 h=21 0.04318766

#$`90`
#   rn k    h      value
#6  90 7 h=19 0.02869149
#12 90 7 h=13 0.02022585
#18 90 7 h=21 0.04510606

As mentioned above, it is better to have unique row names. So, we will leave the 'rn' as a column

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for answering so fast. It seems to be false because one column isn't correctly. The column h contains factors. The result would be h factor,1 factor2, factor,1
@rook1996 I updated the answer from changing sapply to lapply. Can you please check

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.