Join 3 Columns of Different Lengths in R

Joining different length data frames with different columns in R

Seems like you're just looking for a simple left_join. This can be done via dplyr with

left_join(df2, df1)

which will only return rows where df2 and df1 match in the timestamp column. (This drops all of the extra observations in df1).

A base R implementation is:

merge(x = df2, y = df1, by = "timestamp", all.x = TRUE)

Merge and fill different length data in R

using merge with parameter all set to TRUE:

tibble1 <- read.table(text="
x y
a 5
b 10
c 15
d 25",header=TRUE,stringsAsFactors=FALSE)

tibble2 <- read.table(text="
x z
a 7
c 23
d 20",header=TRUE,stringsAsFactors=FALSE)


merge(tibble1,tibble2,all=TRUE)

x y z
1 a 5 7
2 b 10 NA
3 c 15 23
4 d 25 20

Or dplyr::full_join(tibble1,tibble2) for the same effect



Related Topics



Leave a reply



Submit