Home > database >  Join list of tibbles with different number of rows into one dataframe
Join list of tibbles with different number of rows into one dataframe

Time:08-24

I have a list of tibbles (48 to be exact) and they have the same number of columns (2) but a different number of rows. The first column is a value, the second column is a count. Each element has a different name. An example is:

tib_list = list(tibble(value = c(11, 21, 22, 23, 33, 41, 42, 81, 82), 
                  count = c(1:9)), 
           tibble(value = c(11, 21, 22, 23, 41, 42, 82), 
                  count = c(1:7)), 
           tibble(value = c(11, 21, 22, 23), 
                  count = c(1:4)))

names(tib_list) = c("Carrots", "Apples", "Pears")

I want to combine all of the tibbles from the list into a single dataframe. The first column of the created dataframe will have all of the possible values from the tibbles and the second column will have the counts. If there is no count for the corresponding value, then it should say NA. I also want each column to be named based on the element name in the list. I want my final output to basically be like this:

test = data.frame(value = c(11, 21, 22, 23, 33, 41, 42, 81, 82), 
            Carrots = c(1, 2, 3, 4, 5, 6, 7, 8, 9), 
            Apples = c(1, 2, 3, 4, NA, 4, 5, 6, NA), 
            Pears = c(1, 2, 3, 4, NA, NA, NA, NA, NA))

print(test)

  value Carrots Apples Pears
1    11       1      1     1
2    21       2      2     2
3    22       3      3     3
4    23       4      4     4
5    33       5     NA    NA
6    41       6      4    NA
7    42       7      5    NA
8    81       8      6    NA
9    82       9     NA    NA

I have tried cbind (error due to different number of rows) and joins. I am just not sure how to get the output I need. Thanks in advance.

CodePudding user response:

We could also do a reduced full_join and change the names:

library(tidyverse)

reduce(tib_list, full_join, by='value') %>%
   rename_with(~c('value', names(tib_list)))

# A tibble: 9 x 4
  value Carrots Apples Pears
  <dbl>   <int>  <int> <int>
1    11       1      1     1
2    21       2      2     2
3    22       3      3     3
4    23       4      4     4
5    33       5     NA    NA
6    41       6      5    NA
7    42       7      6    NA
8    81       8     NA    NA
9    82       9      7    NA
  • Related