Hi, I have a question about %in% and subsettin data frames. Say I need to keep ID 1,2,4,5, 10 from the data frame dat. I can do: dat <- data.frame(ID = 1:10, var = 1:10) someID <- c(1,2,4,5,10) subset(dat, dat$ID %in% someID) Is there a quick way to do the opposite, ie to do a subset that contains all ID but someID? Something like %not in%, which would *remove* lines with ID in someID? I am asking because I need this in a more complex example where there are multiple lines with the same ID (data in long format) and I need to remove selected ID. thanks, MP [[alternative HTML version deleted]]
Well, %in% returns a logical vector...
So
subset(dat, ! ID %in% someID)
Also, from ?subset:
Note
that ?subset? will be evaluated in the data frame, so columns can
be referred to (by name) as variables in the expression
Thus, you don't need 'dat$ID', bur just 'ID' in the subset
argument.
-Erik
MP.Sylvestre at gmail.com wrote:> Hi,
>
> I have a question about %in% and subsettin data frames.
>
> Say I need to keep ID 1,2,4,5, 10 from the data frame dat. I can do:
>
> dat <- data.frame(ID = 1:10, var = 1:10)
> someID <- c(1,2,4,5,10)
> subset(dat, dat$ID %in% someID)
>
> Is there a quick way to do the opposite, ie to do a subset that contains
> all ID but someID? Something like %not in%, which would *remove* lines with
> ID in someID?
>
> I am asking because I need this in a more complex example where there are
> multiple lines with the same ID (data in long format) and I need to remove
> selected ID.
>
> thanks,
>
> MP
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
Hi MP, Try subset(dat, ! dat$ID %in% someID) # ! symbol HTH, Jorge On Fri, Nov 5, 2010 at 10:13 AM, <> wrote:> Hi, > > I have a question about %in% and subsettin data frames. > > Say I need to keep ID 1,2,4,5, 10 from the data frame dat. I can do: > > dat <- data.frame(ID = 1:10, var = 1:10) > someID <- c(1,2,4,5,10) > subset(dat, dat$ID %in% someID) > > Is there a quick way to do the opposite, ie to do a subset that contains > all ID but someID? Something like %not in%, which would *remove* lines with > ID in someID? > > I am asking because I need this in a more complex example where there are > multiple lines with the same ID (data in long format) and I need to remove > selected ID. > > thanks, > > MP > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Any logical value can be negatively compared using !
does:
subset(dat, !(dat$ID %in% someID))
provide what you need?
--------------------------------------
Jonathan P. Daily
Technician - USGS Leetown Science Center
11649 Leetown Road
Kearneysville WV, 25430
(304) 724-4480
"Is the room still a room when its empty? Does the room,
the thing itself have purpose? Or do we, what's the word... imbue it."
- Jubal Early, Firefly
From:
MP.Sylvestre@gmail.com
To:
r-help@r-project.org
Date:
11/05/2010 02:21 PM
Subject:
[R] subsets, %in%
Sent by:
r-help-bounces@r-project.org
Hi,
I have a question about %in% and subsettin data frames.
Say I need to keep ID 1,2,4,5, 10 from the data frame dat. I can do:
dat <- data.frame(ID = 1:10, var = 1:10)
someID <- c(1,2,4,5,10)
subset(dat, dat$ID %in% someID)
Is there a quick way to do the opposite, ie to do a subset that contains
all ID but someID? Something like %not in%, which would *remove* lines
with
ID in someID?
I am asking because I need this in a more complex example where there are
multiple lines with the same ID (data in long format) and I need to remove
selected ID.
thanks,
MP
[[alternative HTML version deleted]]
______________________________________________
R-help@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.
[[alternative HTML version deleted]]
> Say I need to keep ID 1,2,4,5, 10 from the data frame dat. I can do: > dat <- data.frame(ID = 1:10, var = 1:10) > someID <- c(1,2,4,5,10) > subset(dat, dat$ID %in% someID) > Is there a quick way to do the opposite ... >Two operators spring to mind: ! and %nin subset(dat, !(dat$ID %in% someID)) subset(dat, dat$ID %nin% someID) -- Curt Seeliger, Data Ranger Raytheon Information Services - Contractor to ORD seeliger.curt@epa.gov 541/754-4638 [[alternative HTML version deleted]]