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
baptiste auguie
2009-Apr-14 14:56 UTC
[R] subset dataframe by rows using character vector?
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
baptiste auguie
2009-Apr-14 15:11 UTC
[R] subset dataframe by rows using character vector?
glad it was helpful. %in% is a logical operator, so you can use "!" to negate the result (with parentheses), ! ( 4 %in% 1:3) Alternatively, define a new operator, `%ni%` <- Negate(`%in%`) 1 %ni% c(2,1) Next time you ask a follow-up question please send it to the r-help list so others can help you too. HTH, baptiste On 14 Apr 2009, at 16:03, zack holden wrote:> > Yes! that's it. Any chance you can tell me how to select everything > but those plotNames? Basically a reverse subset? I don't know these > character matching functions very well. > > I very much appreciate your help, thanks again. > > Zack > > ---------------------------------------- >> CC: r-help at r-project.org >> From: ba208 at exeter.ac.uk >> To: zack_holden at hotmail.com >> Subject: Re: [R] subset dataframe by rows using character vector? >> Date: Tue, 14 Apr 2009 15:56:45 +0100 >> >> 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 >> ______________________________ >>_____________________________ 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