[R] Merge
arun
smartpink111 at yahoo.com
Wed Apr 17 19:16:19 CEST 2013
HI Janesh,
YOu can use:
library(plyr)
?join_all()
#From the help page:
dfs <- list(
a = data.frame(x = 1:10, a = runif(10)),
b = data.frame(x = 1:10, b = runif(10)),
c = data.frame(x = 1:10, c = runif(10))
)
join_all(dfs)
join_all(dfs, "x")
join_all(dfs, "x")
# x a b c
#1 1 0.7113766 0.1348978 0.1153703
#2 2 0.2520057 0.7249154 0.2362936
#3 3 0.5670157 0.8166805 0.3049683
#4 4 0.7441726 0.4929165 0.6779029
#5 5 0.5616914 0.5272339 0.6202915
#6 6 0.2858429 0.1203205 0.8399356
#7 7 0.9910520 0.1251815 0.4729418
#8 8 0.7079778 0.5465055 0.8951371
#9 9 0.0564100 0.1837211 0.6451289
#10 10 0.7169663 0.1328287 0.2467554
Reduce(function(...) merge(...,by="x"),dfs)
# x a b c
#1 1 0.7113766 0.1348978 0.1153703
#2 2 0.2520057 0.7249154 0.2362936
#3 3 0.5670157 0.8166805 0.3049683
#4 4 0.7441726 0.4929165 0.6779029
#5 5 0.5616914 0.5272339 0.6202915
#6 6 0.2858429 0.1203205 0.8399356
#7 7 0.9910520 0.1251815 0.4729418
#8 8 0.7079778 0.5465055 0.8951371
#9 9 0.0564100 0.1837211 0.6451289
#10 10 0.7169663 0.1328287 0.2467554
A.K.
________________________________
From: Janesh Devkota <janesh.devkota at gmail.com>
To: Farnoosh <farnoosh_81 at yahoo.com>
Cc: arun <smartpink111 at yahoo.com>; R help <r-help at r-project.org>
Sent: Wednesday, April 17, 2013 1:05 PM
Subject: Re: [R] Merge
Hi, I have a quick question here. Lets say he has three data frames and he needs to combine those three data frame using merge. Can we simply use merge to join three data frames ? I remember I had some problem using merge for more than two dataframes.
Thanks.
On Wed, Apr 17, 2013 at 1:05 AM, Farnoosh <farnoosh_81 at yahoo.com> wrote:
Thanks a lot:)
>
>Sent from my iPad
>
>
>On Apr 16, 2013, at 10:15 PM, arun <smartpink111 at yahoo.com> wrote:
>
>> Hi Farnoosh,
>> YOu can use either ?merge() or ?join()
>> DataA<- read.table(text="
>> ID v1
>> 1 10
>> 2 1
>> 3 22
>> 4 15
>> 5 3
>> 6 6
>> 7 8
>> ",sep="",header=TRUE)
>>
>> DataB<- read.table(text="
>> ID v2
>> 2 yes
>> 5 no
>> 7 yes
>> ",sep="",header=TRUE,stringsAsFactors=FALSE)
>>
>> merge(DataA,DataB,by="ID",all.x=TRUE)
>> # ID v1 v2
>> #1 1 10 <NA>
>> #2 2 1 yes
>> #3 3 22 <NA>
>> #4 4 15 <NA>
>> #5 5 3 no
>> #6 6 6 <NA>
>> #7 7 8 yes
>> library(plyr)
>> join(DataA,DataB,by="ID",type="left")
>> # ID v1 v2
>> #1 1 10 <NA>
>> #2 2 1 yes
>> #3 3 22 <NA>
>> #4 4 15 <NA>
>> #5 5 3 no
>> #6 6 6 <NA>
>> #7 7 8 yes
>> A.K.
>>
>>
>>
>>
>>
>> ________________________________
>> From: farnoosh sheikhi <farnoosh_81 at yahoo.com>
>> To: "smartpink111 at yahoo.com" <smartpink111 at yahoo.com>
>> Sent: Wednesday, April 17, 2013 12:52 AM
>> Subject: Merge
>>
>>
>>
>> Hi Arun,
>>
>> I want to merge a data set with another data frame with 2 columns and keep the sample size of the DataA.
>>
>> DataA DataB DataCombine
>> ID v1 ID V2 ID v1 v2
>> 1 10 2 yes 1 10 NA
>> 2 1 5 no 2 1 yes
>> 3 22 7 yes 3 22 NA
>> 4 15 4 15 NA
>> 5 3 5 3 no
>> 6 6 6 6 NA
>> 7 8 7 8 yes
>>
>>
>> Thanks a lot for your help and time.
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list