[R] subset dataframe by rows using character vector?
baptiste auguie
ba208 at exeter.ac.uk
Tue Apr 14 16:56:45 CEST 2009
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
More information about the R-help
mailing list