[R] subset dataframe by rows using character vector?

zack holden zack_holden at hotmail.com
Tue Apr 14 16:20:35 CEST 2009


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



More information about the R-help mailing list