How to select only certain rows when making a new dataframe?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to select only certain rows when making a new dataframe?

mtb954
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).

Thanks! Mark

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

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

Chuck Cleland
THIRD <- merge(FIRST, SECOND, by="ID", all.x=FALSE, all.y=TRUE)

[hidden email] wrote:

> 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).
>
> Thanks! Mark
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

--
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 452-1424 (M, W, F)
fax: (917) 438-0894

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

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

Andy Bunn
In reply to this post by mtb954
> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]]On Behalf Of [hidden email]
> Sent: Tuesday, February 21, 2006 12:54 PM
> To: [hidden email]
> 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,]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

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

Marco Geraci
In reply to this post by mtb954
I think ?merge is what you need. The help page of this
function is very....helpful

Marco

--- [hidden email] wrote:

> 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).
>
> Thanks! Mark
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html