SJ Lin
SJ Lin

Reputation: 19

Merging Multiple (and different datasets)

I'd like to merge multiple (around ten) datasets in R. Quite a few of the datasets are different from each other, so I don't need to match them by row name or anything. I'd just like to paste them side by side, on a single dataframe so I can export them into a single sheet. For instance, I have the following two datasets:

Month Engagement Test
Jan 51 1
Feb 123 2
Variable Engagement
Hot 412
Cold 4124
Warm 4fd4

I'd simply like to put them side by side (as in left and right) in a single data frame for exporting purposes, like this:

Month Engagement Test Variable Engagement
Jan 51 1 Hot 412
Feb 123 2 Cold 4124
NA NA NA Warm 4fd4

Is there any way to accomplish this? It might seem like a strange request, but do let me know if I should provide any more info! Thank you so much.

Upvotes: 0

Views: 125

Answers (2)

Musebe Ivan
Musebe Ivan

Reputation: 182

Index both data then merge by the index and drop the index:

df1 <- read.csv("Book1.csv", header = TRUE, na.strings = "")
df2 <- read.csv("Book2.csv", header = TRUE, na.strings = "")

# Assign index to the dataframe
rownames(df1) <- 1:nrow(df1)
rownames(df2) <- 1:nrow(df2)

# Merge by index:
merged <- merge(df1, df2, by=0, all=TRUE) %>% 
  select(-1)
merged

Output:

  Month Engagement Test Variable Engagement
1   Jan         51    1      Hot        412
2   Feb        123    2     Cold       4124
3  <NA>         NA   NA     Warm       4fd4

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 388797

Put the data in a list. Find the max number of rows from the list. For each dataframe subset the rows, dataframe with lower number of rows will be appended with NA's.

data <- list(df1, df2)
n <- seq_len(max(sapply(data, nrow)))
result <- do.call(cbind, lapply(data, `[`, n, ))
result

#   Month Engagement Test Variable Engagement
#1    Jan         51    1      Hot        412
#2    Feb        123    2     Cold       4124
#NA  <NA>         NA   NA     Warm       4fd4

Upvotes: 4

Related Questions