Dear UseRs, Let us assume that I have data.frame named as dt. dt is as follows: a b c d 1 3 4 5 2 3 3 2 1 3 4 2 3 2 4 5 4 5 3 6 3 2 5 7 2 5 7 8 ..................... ..................... I want to subset dt with fileds a having 2 or 3 or 4, and I wrote following code. dt[dt$a == 2 | dt$a == 3 | dt$a == 4,] Is there more efficient way for subset? Thanks in advance.
Dong-hyun Oh wrote:> Dear UseRs, > > Let us assume that I have data.frame named as dt. > > dt is as follows: > > a b c d > 1 3 4 5 > 2 3 3 2 > 1 3 4 2 > 3 2 4 5 > 4 5 3 6 > 3 2 5 7 > 2 5 7 8 > ..................... > ..................... > > I want to subset dt with fileds a having 2 or 3 or 4, and I wrote > following code. > > dt[dt$a == 2 | dt$a == 3 | dt$a == 4,] > > Is there more efficient way for subset?subset(dt, a %in% 2:4)> Thanks in advance. > > ______________________________________________ > 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.-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
Dimitris Rizopoulos
2007-Oct-24 09:58 UTC
[R] subset data.frame with constraint of many values?
try this:
dt[dt$a %in% 2:4, ]
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
----- Original Message -----
From: "Dong-hyun Oh" <r.arecibo at gmail.com>
To: "R-help" <r-help at stat.math.ethz.ch>
Sent: Wednesday, October 24, 2007 11:44 AM
Subject: [R] subset data.frame with constraint of many values?
> Dear UseRs,
>
> Let us assume that I have data.frame named as dt.
>
> dt is as follows:
>
> a b c d
> 1 3 4 5
> 2 3 3 2
> 1 3 4 2
> 3 2 4 5
> 4 5 3 6
> 3 2 5 7
> 2 5 7 8
> .....................
> .....................
>
> I want to subset dt with fileds a having 2 or 3 or 4, and I wrote
> following code.
>
> dt[dt$a == 2 | dt$a == 3 | dt$a == 4,]
>
> Is there more efficient way for subset?
>
> Thanks in advance.
>
> ______________________________________________
> 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.
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm