Quantcast

extract subset of data.frame

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

extract subset of data.frame

syrvn
Hello,


consider the following data.frame df and vector v

df <- data.frame(group = c("A","B","C","D"), value = c(1,2,3,4))
v <- c(2,3)

How can I return a sub data.frame which has only the rows left where value matches v

df:

group value
B 2
C 3


Cheers
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: extract subset of data.frame

Sarah Goslee
> df[df$value %in% v,]
  group value
2     B     2
3     C     3


df is a function; you're better off not using that name for your
dataframe.

Sarah

On Thu, Feb 23, 2012 at 10:48 AM, syrvn <[hidden email]> wrote:

> Hello,
>
>
> consider the following data.frame df and vector v
>
> df <- data.frame(group = c("A","B","C","D"), value = c(1,2,3,4))
> v <- c(2,3)
>
> How can I return a sub data.frame which has only the rows left where value
> matches v
>
> df:
>
> group value
> B 2
> C 3
>
>
> Cheers
>
> --


--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: extract subset of data.frame

Michael Weylandt
In reply to this post by syrvn
df[value %in% v, ]

Michael

On Thu, Feb 23, 2012 at 10:48 AM, syrvn <[hidden email]> wrote:

> Hello,
>
>
> consider the following data.frame df and vector v
>
> df <- data.frame(group = c("A","B","C","D"), value = c(1,2,3,4))
> v <- c(2,3)
>
> How can I return a sub data.frame which has only the rows left where value
> matches v
>
> df:
>
> group value
> B 2
> C 3
>
>
> Cheers
>
> --
> View this message in context: http://r.789695.n4.nabble.com/extract-subset-of-data-frame-tp4414251p4414251.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Loading...