Rene Bults
Rene Bults

Reputation: 145

Adding first column of many dataframes to new dataframes in R

I have a list of 17 dataframes. From this list, i want to "select" the first column for every dataframe and place it into a new dataframe. The wanted result would be that there is created several new dataframes. e.g. the first resulting dataframe contains all first columnn of all 17 dataframes. the second resulting dataframe contains the second column of all 17 dataframes.

I will show you a visual representation of what i want belowenter image description here

I really hope you can help me out with this!

Cheers

Upvotes: 0

Views: 110

Answers (3)

hmhensen
hmhensen

Reputation: 3195

So you would like to grab the first column of each data frame in a list and turn that into a data frame, then do the same for the second column of each data frame, etc. until you reach the last column of each data frame. I'm assuming each data frame has the same dimensions.

Here's a base solution using nested lapply functions. The output is a list of n data.frame, where n is the number of columns in your data frames.

In my example, the list has four data frames of three columns each. Therefore, the output contains three data frames of four columns each.

lapply(1:ncol(l[[1]]), function(y) as.data.frame(sapply(l, function(x) x[, y])))

[[1]]
  V1 V2 V3 V4
1  1  1  1  1
2  2  2  2  2
3  3  3  3  3
4  4  4  4  4
5  5  5  5  5

[[2]]
  V1 V2 V3 V4
1  2  2  2  2
2  3  3  3  3
3  4  4  4  4
4  5  5  5  5
5  6  6  6  6

[[3]]
  V1 V2 V3 V4
1  3  3  3  3
2  4  4  4  4
3  5  5  5  5
4  6  6  6  6
5  7  7  7  7

Sample data:

l <- list(data.frame(a = c(1:5), b = c(2:6), c = c(3:7)), data.frame(a = c(1:5), b = c(2:6), c = c(3:7)), data.frame(a = c(1:5), b = c(2:6), c = c(3:7)), data.frame(a = c(1:5), b = c(2:6), c = c(3:7)))

Upvotes: 1

Uwe
Uwe

Reputation: 42544

If I understand correctly, the OP wants to transpose the data in a list of k data frames each of dimension n rows times m columns into a list of m data.frames each of dimension n rows times k columns.

The list of data frames

lst <- rep(list(mtcars[1:4, 1:5]), 3L)
lst
[[1]]
                mpg cyl disp  hp drat
Mazda RX4      21.0   6  160 110 3.90
Mazda RX4 Wag  21.0   6  160 110 3.90
Datsun 710     22.8   4  108  93 3.85
Hornet 4 Drive 21.4   6  258 110 3.08

[[2]]
                mpg cyl disp  hp drat
Mazda RX4      21.0   6  160 110 3.90
Mazda RX4 Wag  21.0   6  160 110 3.90
Datsun 710     22.8   4  108  93 3.85
Hornet 4 Drive 21.4   6  258 110 3.08

[[3]]
                mpg cyl disp  hp drat
Mazda RX4      21.0   6  160 110 3.90
Mazda RX4 Wag  21.0   6  160 110 3.90
Datsun 710     22.8   4  108  93 3.85
Hornet 4 Drive 21.4   6  258 110 3.08

is transformed by

library(magrittr)
purrr::transpose(lst) %>% 
  lapply(data.table::as.data.table)

into

$mpg
     V1   V2   V3
1: 21.0 21.0 21.0
2: 21.0 21.0 21.0
3: 22.8 22.8 22.8
4: 21.4 21.4 21.4

$cyl
   V1 V2 V3
1:  6  6  6
2:  6  6  6
3:  4  4  4
4:  6  6  6

$disp
    V1  V2  V3
1: 160 160 160
2: 160 160 160
3: 108 108 108
4: 258 258 258

$hp
    V1  V2  V3
1: 110 110 110
2: 110 110 110
3:  93  93  93
4: 110 110 110

$drat
     V1   V2   V3
1: 3.90 3.90 3.90
2: 3.90 3.90 3.90
3: 3.85 3.85 3.85
4: 3.08 3.08 3.08

as.data.table() is used here because it names the columns automatically.

Upvotes: 1

A. Suliman
A. Suliman

Reputation: 13125

Using purrr::map_dfc

library(purrr)
lst<-list(iris, iris)
fstcol <- map_dfc(lst, ~dplyr::select(.x, 1) %>% head)
sndcol <- map_dfc(lst, ~dplyr::select(.x, 2) %>% head)

Upvotes: 1

Related Questions