[R] An R clause to bind dataframes under certain contions
Hi netters, Suppose I have two data frames X and Y. X has three colnames A, B and C. Y has three colnames A,B and D. I want to combine them into one matrix, joining the rows having the same A and B values (X$A==Y$A and X$B = Y$B). So the resulting dataframe has four variables/columns: A,B,C and D. I was wondering what's the best way to do it in R. Could anyone give me some advice? Thanks! Zhihua Li _ ÊÖ»úÒ²ÄÜÉÏ MSN ÁÄÌìÁË£¬¿ìÀ´ÊÔÊÔ°É£¡ [[alternative HTML version deleted]] __ R-help@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.
Re: [R] An R clause to bind dataframes under certain contions
Try this: merge(x, y, all=T) On 26/01/2008, zhihuali [EMAIL PROTECTED] wrote: Hi netters, Suppose I have two data frames X and Y. X has three colnames A, B and C. Y has three colnames A,B and D. I want to combine them into one matrix, joining the rows having the same A and B values (X$A==Y$A and X$B = Y$B). So the resulting dataframe has four variables/columns: A,B,C and D. I was wondering what's the best way to do it in R. Could anyone give me some advice? Thanks! Zhihua Li _ 手机也能上 MSN 聊天了,快来试试吧! [[alternative HTML version deleted]] __ R-help@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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O __ R-help@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.
Re: [R] An R clause to bind dataframes under certain contions
Hi, Huali, there is another way to do so library(sqldf) wanted - sqldf(select * from X inner join Y on X.A = Y.A and X.B = Y.A) 2008/1/26 zhihuali [EMAIL PROTECTED]: Hi netters, Suppose I have two data frames X and Y. X has three colnames A, B and C. Y has three colnames A,B and D. I want to combine them into one matrix, joining the rows having the same A and B values (X$A==Y$A and X$B = Y$B). So the resulting dataframe has four variables/columns: A,B,C and D. I was wondering what's the best way to do it in R. Could anyone give me some advice? Thanks! Zhihua Li _ 手机也能上 MSN 聊天了,快来试试吧! [[alternative HTML version deleted]] __ R-help@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. -- === WenSui Liu Statistical Project Manager ChoicePoint Precision Marketing (http://spaces.msn.com/statcompute/blog) === __ R-help@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.
Re: [R] An R clause to bind dataframes under certain contions
Henrique Dallazuanna [EMAIL PROTECTED] wrote in news:[EMAIL PROTECTED]: Try this: merge(x, y, all=T) That will give NA's in the non-matching rows, which may be what the OP wanted. If, on the otherhand, only the rows with matches were desired, the usage might be: A.B - merge(X, Y, by = c(A,B)) A.B mymat-data.frame( x=c(1,1,1,2,2,2,3,3,3), y=c(1,2,3,1,2,3,1,2,3), z=c(1,2,3,4,5,6,7,8,9)) mymat2-data.frame( x=c(1,1,1,2,2,2,3,3,3), y=c(1,2,3,1,2,3,1,4,3), zz=c(1,7,3,5,5,6,10,8,20)) mymerge1 - merge(mymat, mymat2, all=TRUE) mymerge1 x y z zz 1 1 1 1 1 2 1 2 2 7 3 1 3 3 3 4 2 1 4 5 5 2 2 5 5 6 2 3 6 6 7 3 1 7 10 8 3 2 8 NA 9 3 3 9 20 10 3 4 NA 8 mymerge2 - merge(mymat, mymat2, by=c(x,y)) mymerge2 x y z zz 1 1 1 1 1 2 1 2 2 7 3 1 3 3 3 4 2 1 4 5 5 2 2 5 5 6 2 3 6 6 7 3 1 7 10 8 3 3 9 20 -- David Winsemius On 26/01/2008, zhihuali [EMAIL PROTECTED] wrote: Hi netters, Suppose I have two data frames X and Y. X has three colnames A, B and C. Y has three colnames A,B and D. I want to combine them into one matrix, joining the rows having the same A and B values (X$A==Y$A and X$B = Y$B). So the resulting dataframe has four variables/columns: A,B,C and D. I was wondering what's the best way to do it in R. Could anyone give me some advice? Thanks! Zhihua Li __ R-help@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.
Re: [R] An R clause to bind dataframes under certain contions
On 26/01/2008, David Winsemius [EMAIL PROTECTED] wrote: Henrique Dallazuanna [EMAIL PROTECTED] wrote in news:[EMAIL PROTECTED]: Try this: merge(x, y, all=T) That will give NA's in the non-matching rows, which may be what the OP wanted. If, on the otherhand, only the rows with matches were desired, the usage might be: In this case omit 'all' argument. mymerge1 - merge(mymat, mymat2) A.B - merge(X, Y, by = c(A,B)) A.B mymat-data.frame( x=c(1,1,1,2,2,2,3,3,3), y=c(1,2,3,1,2,3,1,2,3), z=c(1,2,3,4,5,6,7,8,9)) mymat2-data.frame( x=c(1,1,1,2,2,2,3,3,3), y=c(1,2,3,1,2,3,1,4,3), zz=c(1,7,3,5,5,6,10,8,20)) mymerge1 - merge(mymat, mymat2, all=TRUE) mymerge1 x y z zz 1 1 1 1 1 2 1 2 2 7 3 1 3 3 3 4 2 1 4 5 5 2 2 5 5 6 2 3 6 6 7 3 1 7 10 8 3 2 8 NA 9 3 3 9 20 10 3 4 NA 8 mymerge2 - merge(mymat, mymat2, by=c(x,y)) mymerge2 x y z zz 1 1 1 1 1 2 1 2 2 7 3 1 3 3 3 4 2 1 4 5 5 2 2 5 5 6 2 3 6 6 7 3 1 7 10 8 3 3 9 20 -- David Winsemius On 26/01/2008, zhihuali [EMAIL PROTECTED] wrote: Hi netters, Suppose I have two data frames X and Y. X has three colnames A, B and C. Y has three colnames A,B and D. I want to combine them into one matrix, joining the rows having the same A and B values (X$A==Y$A and X$B = Y$B). So the resulting dataframe has four variables/columns: A,B,C and D. I was wondering what's the best way to do it in R. Could anyone give me some advice? Thanks! Zhihua Li __ R-help@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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O __ R-help@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.
Re: [R] An R clause to bind dataframes under certain contions
Have a look at rbind.fill() in the reshape library. This will return a data.frame not a matrix but you can always convert it to a matrix. --- zhihuali [EMAIL PROTECTED] wrote: Hi netters, Suppose I have two data frames X and Y. X has three colnames A, B and C. Y has three colnames A,B and D. I want to combine them into one matrix, joining the rows having the same A and B values (X$A==Y$A and X$B = Y$B). So the resulting dataframe has four variables/columns: A,B,C and D. I was wondering what's the best way to do it in R. Could anyone give me some advice? Thanks! Zhihua Li _ ÊÖ»úÒ²ÄÜÉÏ MSN ÁÄÌìÁË£¬¿ìÀ´ÊÔÊÔ°É£¡ [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.