jpm miao
2013-May-10 02:06 UTC
[R] How can I extract part of the data with a selection criterion?
Hi, As an example, how can I get the data such that field a of ab, ab["a"], equals 3? I expect the answer to be the union of 2 and 4, as Thanks,> a<-c(1,3,4,3,5,6,5) > b<-c(2,4,6,7,3,1,2) > ab<-data.frame(a,b) > aba b 1 1 2 2 3 4 3 4 6 4 3 7 5 5 3 6 6 1 7 5 2> ab[a==3]Error in `[.data.frame`(ab, a == 3) : undefined columns selected> ab[ab["a"]==3][1] 3 3 4 7 [[alternative HTML version deleted]]
Patrick Coulombe
2013-May-10 02:13 UTC
[R] How can I extract part of the data with a selection criterion?
Hi, Try ab[a==3,] You're selecting only rows for which a==3 (before the comma), and all columns (empty condition for column index, after the comma) Patrick 2013/5/9 jpm miao <miaojpm at gmail.com>:> Hi, > > As an example, how can I get the data such that field a of ab, ab["a"], > equals 3? I expect the answer to be the union of 2 and 4, as > > Thanks, > >> a<-c(1,3,4,3,5,6,5) >> b<-c(2,4,6,7,3,1,2) >> ab<-data.frame(a,b) >> ab > a b > 1 1 2 > 2 3 4 > 3 4 6 > 4 3 7 > 5 5 3 > 6 6 1 > 7 5 2 > >> ab[a==3] > Error in `[.data.frame`(ab, a == 3) : undefined columns selected >> ab[ab["a"]==3] > [1] 3 3 4 7 > > [[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.
David Winsemius
2013-May-10 02:31 UTC
[R] How can I extract part of the data with a selection criterion?
On May 9, 2013, at 7:06 PM, jpm miao wrote:> Hi, > > As an example, how can I get the data such that field a of ab, ab["a"], > equals 3? I expect the answer to be the union of 2 and 4, as > > Thanks, > >> a<-c(1,3,4,3,5,6,5) >> b<-c(2,4,6,7,3,1,2) >> ab<-data.frame(a,b) >> ab > a b > 1 1 2 > 2 3 4 > 3 4 6 > 4 3 7 > 5 5 3 > 6 6 1 > 7 5 2 > >> ab[a==3] > Error in `[.data.frame`(ab, a == 3) : undefined columns selected >> ab[ab["a"]==3] > [1] 3 3 4 7 >Try; ab[ ab[["a"]]==3, ] And then read ?Extract to help drive home the point the ab["a"] is a list with one element and ab[["a"]] is a vector with 7 elements. -- David Winsemius Alameda, CA, USA
Reasonably Related Threads
- How can I extract part of the data in a panel dataset?
- Why can't R understand if(num!=NA)?
- Definition of "lag" is opposite in ts and xts objects!
- How can I tabulate time series data (in RStudio or any other R editor)?
- How to print the frequency table (produced by the command "table" to Excel