hi, I have a question about merging two files. For example, I have two files, the first file is like the following: id trait1 1 10.2 2 11.1 3 9.7 6 10.2 7 8.9 10 9.7 11 10.2 The second file is like the following: id trait2 1 9.8 2 10.8 4 7.8 5 9.8 6 10.1 12 10.2 13 10.1 now I want to merge the two files by the variable "id", I only want to keep the "id"s which show up in the first file. Even the "id" does not show up in the second file, it doesn't matter, I can keep the missing values. So my question is: how can I merge the two files and keep only the rows whose "id" show up in the first file? I know how to do it is SAS, just use the following code: merge data1(in=in1) data2(in=in2); by id; if in1; but I really have no idea about how to do it in R. thank you in advance, karean -- View this message in context: http://n4.nabble.com/merging-issue-tp1013356p1013356.html Sent from the R help mailing list archive at Nabble.com.
Try the merge function ?merge in1 = "id trait1 1 10.2 2 11.1 3 9.7 6 10.2 7 8.9 10 9.7 11 10.2 " in2 = "id trait2 1 9.8 2 10.8 4 7.8 5 9.8 6 10.1 12 10.2 13 10.1 " data1 = read.table(textConnection(in1), header=T) data2 = read.table(textConnection(in2), header=T) mymerge = merge(data1,data2,all.x=TRUE) print(mymerge) karena wrote:> > hi, I have a question about merging two files. > For example, I have two files, the first file is like the following: > > id trait1 > 1 10.2 > 2 11.1 > 3 9.7 > 6 10.2 > 7 8.9 > 10 9.7 > 11 10.2 > > The second file is like the following: > id trait2 > 1 9.8 > 2 10.8 > 4 7.8 > 5 9.8 > 6 10.1 > 12 10.2 > 13 10.1 > > now I want to merge the two files by the variable "id", I only want to > keep the "id"s which show up in the first file. Even the "id" does not > show up in the second file, it doesn't matter, I can keep the missing > values. So my question is: how can I merge the two files and keep only the > rows whose "id" show up in the first file? > I know how to do it is SAS, just use the following code: > merge data1(in=in1) data2(in=in2); > by id; > if in1; > > but I really have no idea about how to do it in R. > > thank you in advance, > > karean >-- View this message in context: http://n4.nabble.com/merging-issue-tp1013356p1013375.html Sent from the R help mailing list archive at Nabble.com.
Hi Karean, If your first object is called obj1 and the second called obj2, then:> merge(obj1, obj2, all.x=TRUE)id trait1 trait2 1 1 10.2 9.8 2 2 11.1 10.8 3 3 9.7 NA 4 6 10.2 10.1 5 7 8.9 NA 6 10 9.7 NA 7 11 10.2 NA Hope this helps, Adrian On Wednesday 13 January 2010, karena wrote:> hi, I have a question about merging two files. > For example, I have two files, the first file is like the following: > > id trait1 > 1 10.2 > 2 11.1 > 3 9.7 > 6 10.2 > 7 8.9 > 10 9.7 > 11 10.2 > > The second file is like the following: > id trait2 > 1 9.8 > 2 10.8 > 4 7.8 > 5 9.8 > 6 10.1 > 12 10.2 > 13 10.1 > > now I want to merge the two files by the variable "id", I only want to keep > the "id"s which show up in the first file. Even the "id" does not show up > in the second file, it doesn't matter, I can keep the missing values. So > my question is: how can I merge the two files and keep only the rows whose > "id" show up in the first file? > I know how to do it is SAS, just use the following code: > merge data1(in=in1) data2(in=in2); > by id; > if in1; > > but I really have no idea about how to do it in R. > > thank you in advance, > > karean >-- Adrian Dusa Romanian Social Data Archive 1, Schitu Magureanu Bd. 050025 Bucharest sector 5 Romania Tel.:+40 21 3126618 \ +40 21 3120210 / int.101 Fax: +40 21 3158391
Did you consider to look at the help page for merge? h At 22:01 13.01.2010, karena wrote:>hi, I have a question about merging two files. >For example, I have two files, the first file is like the following: > >id trait1 >1 10.2 >2 11.1 >3 9.7 >6 10.2 >7 8.9 >10 9.7 >11 10.2 > >The second file is like the following: >id trait2 >1 9.8 >2 10.8 >4 7.8 >5 9.8 >6 10.1 >12 10.2 >13 10.1 > >now I want to merge the two files by the variable "id", I only want to keep >the "id"s which show up in the first file. Even the "id" does not show up in >the second file, it doesn't matter, I can keep the missing values. So my >question is: how can I merge the two files and keep only the rows whose "id" >show up in the first file? >I know how to do it is SAS, just use the following code: >merge data1(in=in1) data2(in=in2); >by id; >if in1; > >but I really have no idea about how to do it in R. > >thank you in advance, > >karean >-- >View this message in context: >http://n4.nabble.com/merging-issue-tp1013356p1013356.html >Sent from the R help mailing list archive at Nabble.com. > >______________________________________________ >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.
Possibly Parallel Threads
- [PATCH] Optimize silk_warped_autocorrelation_FIX() for ARM NEON
- [PATCH] Optimize silk_warped_autocorrelation_FIX() for ARM NEON
- [PATCH] Optimize silk_warped_autocorrelation_FIX() for ARM NEON
- [PATCH] Optimize silk_warped_autocorrelation_FIX() for ARM NEON
- GVN / Alias Analysis issue with llvm.masked.scatter/gather intrinsics