Reputation: 757
I can combine two data
df1 <- read.table(text="info var1m pos_var1
1 C001 NA NA
2 C002 NA NA
3 C003 0.1790000 1
4 C004 NA NA
5 C007 0.0645000 1
6 C010 0.3895000 1
11 C016 0.2805000 1
12 C017 0.7805001 1", header=T, stringsAsFactors=F)
and this
df2 <- read.table(text="info var1 var2
1 C003 0.1790 1.1305
2 C007 0.0645 0.2985
3 C010 0.3895 0.1705
4 C016 0.1740 0.3980
5 C017 0.4840 0.3375
6 C022 0.1740 0.3980
7 C023 0.4840 0.3375", header=T, stringsAsFactors=F)
df3 <- read.table(text="info var1 var2
5 C017 0.4840 0.3375
6 C022 0.1740 0.3980
7 C023 0.4840 0.3375", header=T, stringsAsFactors=F)
I just give an example of three data sets that I am trying to combine. I should not combine them two by two because then I lose some info
for example in order to combine two of them I can do the following
mydf <- inner_join(df1,df2,'info')
then combine the third , fourth etc dataset into that. However, if I don't have mutual data in df1,df2 and combine , it will be discarded but it might have similar data in df1 and df3.
Is there a way that I combine as many data as I have without losing info but keep all mutual info across all data ?
Upvotes: 1
Views: 44
Reputation: 66765
library(dplyr)
mydf <- df1 %>%
full_join(df2) %>%
full_join(df3)
Output:
> mydf
info var1m pos_var1 var1 var2
1 C001 NA NA NA NA
2 C002 NA NA NA NA
3 C003 0.1790000 1 0.1790 1.1305
4 C004 NA NA NA NA
5 C007 0.0645000 1 0.0645 0.2985
6 C010 0.3895000 1 0.3895 0.1705
7 C016 0.2805000 1 0.1740 0.3980
8 C017 0.7805001 1 0.4840 0.3375
9 C022 NA NA 0.1740 0.3980
10 C023 NA NA 0.4840 0.3375
Upvotes: 1