Skip to content
Back to formatted view

Raw Message

Message-ID: <836CFD6C-6B3E-4D44-8B2C-EE0A7F581321@exeter.ac.uk>
Date: 2009-04-14T14:56:45Z
From: Baptiste Auguie
Subject: subset dataframe by rows using character vector?
In-Reply-To: <BAY125-W18B7BF7784AD49F6AB5014877C0@phx.gbl>

Is this what you want?

plotNames <- c("plot1", "plot2", "plot3")     # plot is probably best  
left as the name of the base function
full.data[full.data$PLOTID %in% plotNames, ] # note the comma


HTH,

baptiste

On 14 Apr 2009, at 15:20, zack holden wrote:

>
> Dear List,
>
> I'm stuck on what seems like a simple indexing problem, I'd be very  
> grateful to anyone willing to help me out.
>
> I queried a dataframe which returns a character vector called  
> "plot". I have another dataframe from which I want to subset or  
> select only those rows that match "plot". I've tried subset, and  
> also the "which" command.
>
>
> plot <- c("plot1", "plot2", "plot3")                   # character  
> vector used to select rows from data
>
> # create fake data from to try out subset
> v1 <- c(2,5,6,4,3)
> PLOTID <- c("plot1", "plot2", "plot3", "plot4", "plot5")
> full.data <- cbind(v1, PLOTID)
> full.data <- as.data.frame(full.data)
>
> # first attempt
> test <- which(full.data$PLOTID == plot)
>
> # second attempt
> test <- full.data[full.data$PLOTID == plot]
>
>
> I'm sure there is a simple solution here, but I'm unable to figure  
> it out.
>
> Thanks for any suggestions,
>
> Zack
> ______________________________________________
> 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.

_____________________________

Baptiste Augui?

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag