Home > OS >  How to iteratively add rows of one tibble to tibbles within a list of tibbles?
How to iteratively add rows of one tibble to tibbles within a list of tibbles?

Time:08-10

Given a list of tibbles

library(dplyr)
library(purrr)

ltb <- list(tibble(a=1:10, b=1:10), tibble(a=1:10, b=1:10), tibble(a=1:10, b=1:10))

map(ltb, ~head(., 2))
[[1]]
# A tibble: 2 × 2
      a     b
  <int> <int>
1     1     1
2     2     2

[[2]]
# A tibble: 2 × 2
      a     b
  <int> <int>
1     1     1
2     2     2

[[3]]
# A tibble: 2 × 2
      a     b
  <int> <int>
1     1     1
2     2     2

and another single tibble whose number of rows matches the number of elements in the above list

tib <- tibble(data1 = letters[1:3], data2 = LETTERS[1:3], data3 = letters[1:3])

> tib
# A tibble: 3 × 3
  data1 data2 data3
  <chr> <chr> <chr>
1 a     A     a    
2 b     B     b    
3 c     C     c    

how can I bind the first row of tib to the first tibble in ltb, the second row of tib to the second tibble in ltb? Obviously, this should recycle the rows in tib to (dynamically) match the number of rows in each tibble in ltb.

So the result should look something like this

map(newltb, ~head(., 3))

[[1]]
# A tibble: 3 × 2
      a     b data1 data2 data3
  <int> <int> <chr> <chr> <chr>
1     1     1   a     A     a    
2     2     2   a     A     a    
3     3     3   a     A     a    

[[2]]
# A tibble: 3 × 2
      a     b data1 data2 data3
  <int> <int> <chr> <chr> <chr>
1     1     1   b     B     b    
2     2     2   b     B     b    
3     3     3   b     B     b    

[[3]]
# A tibble: 3 × 2
      a     b data1 data2 data3
  <int> <int> <chr> <chr> <chr>
1     1     1   c     C     c    
2     2     2   c     C     c    
3     3     3   c     C     c    

I struggle whether to use map2 or pmap2, neither one have worked for me.

CodePudding user response:

You could split tib by rows and use map2 and bind_cols like so:

library(dplyr, warn = FALSE)
library(purrr)

ltb <- list(tibble(a=1:10, b=1:10), tibble(a=1:10, b=1:10), tibble(a=1:10, b=1:10))

tib <- tibble(data1 = letters[1:3], data2 = LETTERS[1:3], data3 = letters[1:3])

tib_split <- tib %>%
  split(seq(nrow(.)))

map2(ltb, tib_split, bind_cols)
#> [[1]]
#> # A tibble: 10 × 5
#>        a     b data1 data2 data3
#>    <int> <int> <chr> <chr> <chr>
#>  1     1     1 a     A     a    
#>  2     2     2 a     A     a    
#>  3     3     3 a     A     a    
#>  4     4     4 a     A     a    
#>  5     5     5 a     A     a    
#>  6     6     6 a     A     a    
#>  7     7     7 a     A     a    
#>  8     8     8 a     A     a    
#>  9     9     9 a     A     a    
#> 10    10    10 a     A     a    
#> 
#> [[2]]
#> # A tibble: 10 × 5
#>        a     b data1 data2 data3
#>    <int> <int> <chr> <chr> <chr>
#>  1     1     1 b     B     b    
#>  2     2     2 b     B     b    
#>  3     3     3 b     B     b    
#>  4     4     4 b     B     b    
#>  5     5     5 b     B     b    
#>  6     6     6 b     B     b    
#>  7     7     7 b     B     b    
#>  8     8     8 b     B     b    
#>  9     9     9 b     B     b    
#> 10    10    10 b     B     b    
#> 
#> [[3]]
#> # A tibble: 10 × 5
#>        a     b data1 data2 data3
#>    <int> <int> <chr> <chr> <chr>
#>  1     1     1 c     C     c    
#>  2     2     2 c     C     c    
#>  3     3     3 c     C     c    
#>  4     4     4 c     C     c    
#>  5     5     5 c     C     c    
#>  6     6     6 c     C     c    
#>  7     7     7 c     C     c    
#>  8     8     8 c     C     c    
#>  9     9     9 c     C     c    
#> 10    10    10 c     C     c

CodePudding user response:

In base R, can use a for loop

for(i in seq_along(ltb)) ltb[[i]][names(tib)] <- tib[i,]

-output

> ltb
[[1]]
# A tibble: 10 × 5
       a     b data1 data2 data3
   <int> <int> <chr> <chr> <chr>
 1     1     1 a     A     a    
 2     2     2 a     A     a    
 3     3     3 a     A     a    
 4     4     4 a     A     a    
 5     5     5 a     A     a    
 6     6     6 a     A     a    
 7     7     7 a     A     a    
 8     8     8 a     A     a    
 9     9     9 a     A     a    
10    10    10 a     A     a    

[[2]]
# A tibble: 10 × 5
       a     b data1 data2 data3
   <int> <int> <chr> <chr> <chr>
 1     1     1 b     B     b    
 2     2     2 b     B     b    
 3     3     3 b     B     b    
 4     4     4 b     B     b    
 5     5     5 b     B     b    
 6     6     6 b     B     b    
 7     7     7 b     B     b    
 8     8     8 b     B     b    
 9     9     9 b     B     b    
10    10    10 b     B     b    

[[3]]
# A tibble: 10 × 5
       a     b data1 data2 data3
   <int> <int> <chr> <chr> <chr>
 1     1     1 c     C     c    
 2     2     2 c     C     c    
 3     3     3 c     C     c    
 4     4     4 c     C     c    
 5     5     5 c     C     c    
 6     6     6 c     C     c    
 7     7     7 c     C     c    
 8     8     8 c     C     c    
 9     9     9 c     C     c    
10    10    10 c     C     c    
  • Related