After using the map function, I ended up with a list of tibbles with different number of rows. As suggested in the purr documentation (https://purrr.tidyverse.org/reference/map_dfr.html?q=map_dfr#null), I used list_cbind() to convert them into a single tibble. However, because of their different number of rows, I get an error message.
A simplified example below:
a1 <- tibble(
name1 = c(1,2,3)
)
a2 <- tibble(
name2 = c(1,2,3)
)
a3 <- tibble(
name3 = c(1,2)
)
A <- list(a1, a2, a3)
list_cbind(A)
and I get the following error message:
Error in `list_cbind()`:
! Can't recycle `..1` (size 3) to match `..3` (size 2).
Run `rlang::last_error()` to see where the error occurred.`
I also tried this (size = An optional integer size to ensure that every input has the same size (i.e. number of rows)) but the same error still occurs.
list_cbind(list(a1, a2, a3), size = 2)
Any suggestions how to do it using the tidyverse (or otherwise)?
First calculate the dataframe with multiple rows.
Next go fill the dataframes which have less than the max number of rows with NA values, in the
sapply
I also extended to the case that the dataframes have more than one column.Finally, using
map
I unlisted the dataframes and joined them by columns. (in case they have more than one column it would be advisable to do the operation on the rows and evaluate case by case)