Re: [R] How to select only certain rows when making a new dataframe?

From: Andy Bunn <abunn_at_whrc.org>
Date: Wed 22 Feb 2006 - 05:09:33 EST


> -----Original Message-----
> From: r-help-bounces@stat.math.ethz.ch
> [mailto:r-help-bounces@stat.math.ethz.ch]On Behalf Of mtb954@gmail.com
> Sent: Tuesday, February 21, 2006 12:54 PM
> To: r-help@stat.math.ethz.ch
> Subject: [R] How to select only certain rows when making a new
> dataframe?
>
>
> Dear R-users,
>
> I have two data frames. The "FIRST" data frame has 100 rows, the
> "SECOND" data frame has only 50 rows.
>
> The data frames have different variables in columns ("VAR1," "VAR2,"
> etc) but they share a column called "ID" that contains a unique
> identifer linking the two data frames.
>
> I would like to make a "THIRD" data frame containing just the rows of
> the "FIRST" data frame that match the rows (on "ID") in the "SECOND"
> data frame.
>
> >THIRD=data.frame(FIRST$ID,FIRST$VAR1,FIRST$VAR2)
>
> How can I modify this line to include in "THIRD" just the rows in
> "FIRST" that match the rows in "SECOND"? (i.e., contain the same value
> in the shared "ID" column).

List this?

one <- data.frame(ID = 1:100, Var1 = runif(100), Var2 = runif(100))
two <- data.frame(ID = 26:50, VarA = runif(50), VarB = runif(50))
three <- one[one$ID == two$ID,]

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Feb 22 05:21:09 2006

This archive was generated by hypermail 2.1.8 : Wed 22 Feb 2006 - 09:27:47 EST