MYaseen208
MYaseen208

Reputation: 23898

tidyverse: binding list elements of same dimension

Using reduce(bind_cols), the list elements of same dimension may be combined. However, I would like to know how to combine only same dimension (may be specified dimesion in some way) elements from a list which may have elements of different dimension.

library(tidyverse)

df1 <- data.frame(A1 = 1:10, A2 = 10:1)
df2 <- data.frame(B = 11:30)
df3 <- data.frame(C = 31:40)

ls1 <- list(df1, df3)
ls1

[[1]]
   A1 A2
1   1 10
2   2  9
3   3  8
4   4  7
5   5  6
6   6  5
7   7  4
8   8  3
9   9  2
10 10  1

[[2]]
    C
1  31
2  32
3  33
4  34
5  35
6  36
7  37
8  38
9  39
10 40

ls1 %>%
  reduce(bind_cols)

  A1 A2  C
1   1 10 31
2   2  9 32
3   3  8 33
4   4  7 34
5   5  6 35
6   6  5 36
7   7  4 37
8   8  3 38
9   9  2 39
10 10  1 40

ls2 <- list(df1, df2, df3)
ls2

[[1]]
   A1 A2
1   1 10
2   2  9
3   3  8
4   4  7
5   5  6
6   6  5
7   7  4
8   8  3
9   9  2
10 10  1

[[2]]
    B
1  11
2  12
3  13
4  14
5  15
6  16
7  17
8  18
9  19
10 20
11 21
12 22
13 23
14 24
15 25
16 26
17 27
18 28
19 29
20 30

[[3]]
    C
1  31
2  32
3  33
4  34
5  35
6  36
7  37
8  38
9  39
10 40


ls2 %>%
  reduce(bind_cols)

Error: Can't recycle `..1` (size 10) to match `..2` (size 20).
Run `rlang::last_error()` to see where the error occurred.

Question

Looking for a function to combine all data.frames in a list with an argument of number of rows.

Upvotes: 15

Views: 757

Answers (7)

Anoushiravan R
Anoushiravan R

Reputation: 21908

We can also use Reduce function from base R:

lst <- list(df1, df2, df3)

# First we create id number for each underlying data set

lst |>
  lapply(\(x) {x$id <- 1:nrow(x); 
  x
 }
) -> ls2

Reduce(function(x, y) if(nrow(x) == nrow(y)){
  merge(x, y, by = "id")
} else {
  x
}, ls2)


   id A1 A2  C
1   1  1 10 31
2   2  2  9 32
3   3  3  8 33
4   4  4  7 34
5   5  5  6 35
6   6  6  5 36
7   7  7  4 37
8   8  8  3 38
9   9  9  2 39
10 10 10  1 40

Upvotes: 3

AnilGoyal
AnilGoyal

Reputation: 26218

You may also use if inside reduce if you want to combine similar elements of list (case: when first item in list has priority)

df1 <- data.frame(A1 = 1:10, A2 = 10:1)
df2 <- data.frame(B = 11:30)
df3 <- data.frame(C = 31:40)

ls1 <- list(df1, df3)

ls2 <- list(df1, df2, df3)
library(tidyverse)

reduce(ls2, ~if(nrow(.x) == nrow(.y)){bind_cols(.x, .y)} else {.x})
#>    A1 A2  C
#> 1   1 10 31
#> 2   2  9 32
#> 3   3  8 33
#> 4   4  7 34
#> 5   5  6 35
#> 6   6  5 36
#> 7   7  4 37
#> 8   8  3 38
#> 9   9  2 39
#> 10 10  1 40

Created on 2021-06-09 by the reprex package (v2.0.0)

Upvotes: 4

ThomasIsCoding
ThomasIsCoding

Reputation: 101343

A base R option using tapply + sapply

tapply(
  ls2,
  sapply(ls2, nrow),
  function(x) do.call(cbind, x)
)

gives

$`10`
   A1 A2  C
1   1 10 31
2   2  9 32
3   3  8 33
4   4  7 34
5   5  6 35
6   6  5 36
7   7  4 37
8   8  3 38
9   9  2 39
10 10  1 40

$`20`
    B
1  11
2  12
3  13
4  14
5  15
6  16
7  17
8  18
9  19
10 20
11 21
12 22
13 23
14 24
15 25
16 26
17 27
18 28
19 29
20 30

Upvotes: 4

akrun
akrun

Reputation: 887118

We can use cbind.fill from rowr

library(rowr)
do.call(cbind.fill, c(ls2, fill = NA))

Upvotes: 4

tmfmnk
tmfmnk

Reputation: 39858

One option could be:

map(split(lst, map_int(lst, NROW)), bind_cols)

$`10`
   A1 A2  C
1   1 10 31
2   2  9 32
3   3  8 33
4   4  7 34
5   5  6 35
6   6  5 36
7   7  4 37
8   8  3 38
9   9  2 39
10 10  1 40

$`20`
    B
1  11
2  12
3  13
4  14
5  15
6  16
7  17
8  18
9  19
10 20
11 21
12 22
13 23
14 24
15 25
16 26
17 27
18 28
19 29
20 30

Upvotes: 7

Ronak Shah
Ronak Shah

Reputation: 388982

You can use -

n <- 1:max(sapply(ls2, nrow))
res <- do.call(cbind, lapply(ls2, `[`, n, ,drop = FALSE))
res

#     A1 A2  B  C
#1     1 10 11 31
#2     2  9 12 32
#3     3  8 13 33
#4     4  7 14 34
#5     5  6 15 35
#6     6  5 16 36
#7     7  4 17 37
#8     8  3 18 38
#9     9  2 19 39
#10   10  1 20 40
#NA   NA NA 21 NA
#NA.1 NA NA 22 NA
#NA.2 NA NA 23 NA
#NA.3 NA NA 24 NA
#NA.4 NA NA 25 NA
#NA.5 NA NA 26 NA
#NA.6 NA NA 27 NA
#NA.7 NA NA 28 NA
#NA.8 NA NA 29 NA
#NA.9 NA NA 30 NA

A little-bit shorter with purrr::map_dfc

purrr::map_dfc(ls2, `[`, n, , drop = FALSE)

Upvotes: 5

Matt
Matt

Reputation: 7385

Here's another tidyverse option.

We're creating a dummy ID in each data.frame based on the row_number(), then joining all data.frames by the dummy ID, and then dropping the dummy ID.

ls2 %>%
  map(., ~mutate(.x, id = row_number())) %>% 
  reduce(full_join, by = "id") %>% 
  select(-id)

This gives us:

 A1 A2  B  C
1   1 10 11 31
2   2  9 12 32
3   3  8 13 33
4   4  7 14 34
5   5  6 15 35
6   6  5 16 36
7   7  4 17 37
8   8  3 18 38
9   9  2 19 39
10 10  1 20 40
11 NA NA 21 NA
12 NA NA 22 NA
13 NA NA 23 NA
14 NA NA 24 NA
15 NA NA 25 NA
16 NA NA 26 NA
17 NA NA 27 NA
18 NA NA 28 NA
19 NA NA 29 NA
20 NA NA 30 NA

Upvotes: 3

Related Questions