R - Concatenate Two Dataframes

Concatenate values in two data frames in R

We may use Map to do this on a loop

data.frame(Map(paste, setNames(a, paste0("cat_", names(a))), b, 
MoreArgs = list(sep = "_")))

-output

   cat_x cat_y
1 1_A 5_E
2 2_B 6_F
3 3_C 7_G
4 4_D 8_H

Used sep above in case we want to add a delimiter. Or else by default it will be space

data.frame(Map(paste, setNames(a, paste0("cat_", names(a))), b ))
cat_x cat_y
1 1 A 5 E
2 2 B 6 F
3 3 C 7 G
4 4 D 8 H

Merge and concatenate two data frames in R

You can do this with merge().

merge(df1, df2, by=c("col1", "col2", "id"), all.x=T, all.y=T)

This merges by all common variables, keeping all records in either data frame. Alternatively you can omit the by= argument and R will automatically use all common variables.

As @thelatemail mentioned in a comment, rather than individually specifying all.x=T and all.y=T, you can alternatively use all=T.

Concatenate two dataframes and replacing NA values in R and transform the result in a csv file

You may combine the two datasets using bind_rows and sort the columns putting NA's at the last.

library(dplyr)

bind_rows(x, y) %>%
mutate(ID = row_number(),
across(c(S1, S2), sort, na.last = TRUE))

# ID S1 S2
#1 1 10 21
#2 2 11 22
#3 3 12 23
#4 4 13 24
#5 5 14 25
#6 6 NA 26
#7 7 NA 27

One-liner to concatenate two data frames with a distinguishing column?

It may be easier with bind_rows

library(dplyr)
bind_rows(list(A = df1, B = df2), .id = 'id')

Concatenate/merge dataframes in R into vector type cells

Using some tidyverse, you can invert the lists and then build it all back together.

library(purrr)
library(dplyr)

as_tibble(map2(DF1, DF2, ~ map(transpose(list(.x, .y)), unlist)))

This gets you your data frame of vectors.

# A tibble: 5 x 3
xx yy zz
<list> <list> <list>
1 <int [2]> <int [2]> <chr [2]>
2 <int [2]> <int [2]> <chr [2]>
3 <int [2]> <int [2]> <chr [2]>
4 <int [2]> <int [2]> <chr [2]>
5 <int [2]> <int [2]> <chr [2]>

Breaking this down...

  1. transpose(list(.x, .y)) will flip a paired list of columns inside-out from a list of two vectors to a list of 5 elements (one for each row, each with two list elements in it).
  2. map(transpose(list(.x, .y)), unlist)) will iterate over each of the 5 lists and unlist them back from a list of 2 to a vector of 2.
  3. map2(DF1, DF2, ~ map(transpose(list(.x, .y)), unlist)) will iterate over each column pair from DF1 and DF2 (e.g., xx, yy, zz) doing steps 1 and 2.
  4. as_tibble(map2(DF1, DF2, ~ map(transpose(list(.x, .y)), unlist))) converts the list to a tibble (basically a data.frame).

Another thing you can do is stack the data and then nest() it. You again need a few steps to do it. This would scale better because you could do this with more than 2 data frames.

library(dplyr)
library(tibble)
library(tidyr)

bind_rows(rowid_to_column(DF1),
rowid_to_column(DF2)) %>%
group_by(rowid) %>%
nest(nest_data = -rowid) %>%
unnest_wider(nest_data) %>%
ungroup() %>%
select(-rowid)

This also gets you your data frame of vectors.

# A tibble: 5 x 3
xx yy zz
<list> <list> <list>
1 <int [2]> <int [2]> <chr [2]>
2 <int [2]> <int [2]> <chr [2]>
3 <int [2]> <int [2]> <chr [2]>
4 <int [2]> <int [2]> <chr [2]>
5 <int [2]> <int [2]> <chr [2]>


Related Topics



Leave a reply



Submit