Hello, I want to select in a column of a dataframe all numbers smaller than a value x but when I type in test<-(RSF_EU$AREA<=x) I receiv as answer: > test [1] TRUE FALSE FALSE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE [18] TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE [35] FALSE TRUE TRUE TRUE TRUE FALSE FALSE FALSE TRUE TRUE FALSE TRUE TRUE FALSE FALSE TRUE FALSE [52] TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE FALSE TRUE How can i get the values smaller than x and not the TRUE/FALSE reply? Thanks in advance, Thomas
On Fri, 23 Feb 2007 14:38:56 +0100 Thomas Preuth <preuth at slf.ch> wrote:> Hello, > > I want to select in a column of a dataframe all numbers smaller than a > value x > but when I type in test<-(RSF_EU$AREA<=x) I receiv as answer: > > test > [1] TRUE FALSE FALSE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE > FALSE TRUE TRUE TRUE TRUE TRUE > [18] TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE TRUE TRUE > TRUE TRUE FALSE TRUE TRUE TRUE > [35] FALSE TRUE TRUE TRUE TRUE FALSE FALSE FALSE TRUE TRUE FALSE > TRUE TRUE FALSE FALSE TRUE FALSE > [52] TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE FALSE TRUE > > How can i get the values smaller than x and not the TRUE/FALSE reply?if the dataframe is called RSF_EU, and you want the entire dataframe for those rows, then RSF_EU [ (RSF_EU$AREA <= x ), ] if you want to get only that column vector and nothing else RSF_EU$AREA [ ( RSF_EU$AREA <= x ) ] Such concepts are very well-explained in "An Introduction to R" which you would benefit by reading at the earliest. Ranjan> Thanks in advance, > Thomas > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
RSF_EU$AREA[RSF_EU$AREA<=x] ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Reseach Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx op inbo.be www.inbo.be Do not put your faith in what statistics say until you have carefully considered what they do not say. ~William W. Watt A statistical analysis, properly conducted, is a delicate dissection of uncertainties, a surgery of suppositions. ~M.J.Moroney> -----Oorspronkelijk bericht----- > Van: r-help-bounces op stat.math.ethz.ch [mailto:r-help- > bounces op stat.math.ethz.ch] Namens Thomas Preuth > Verzonden: vrijdag 23 februari 2007 14:39 > Aan: r-help op stat.math.ethz.ch > Onderwerp: [R] TRUE/FALSE as numeric values > > Hello, > > I want to select in a column of a dataframe all numbers smaller than a > value x > but when I type in test<-(RSF_EU$AREA<=x) I receiv as answer: > > test > [1] TRUE FALSE FALSE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE > FALSE TRUE TRUE TRUE TRUE TRUE > [18] TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE TRUE TRUE > TRUE TRUE FALSE TRUE TRUE TRUE > [35] FALSE TRUE TRUE TRUE TRUE FALSE FALSE FALSE TRUE TRUE FALSE > TRUE TRUE FALSE FALSE TRUE FALSE > [52] TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE FALSE TRUE > > How can i get the values smaller than x and not the TRUE/FALSE reply? > > Thanks in advance, > Thomas > > ______________________________________________ > R-help op stat.math.ethz.ch 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.
You can also: test <- RSF_EU[which(RSF_EU$AREA<=x),] On 23/02/07, Thomas Preuth <preuth@slf.ch> wrote:> > Hello, > > I want to select in a column of a dataframe all numbers smaller than a > value x > but when I type in test<-(RSF_EU$AREA<=x) I receiv as answer: > > test > [1] TRUE FALSE FALSE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE > FALSE TRUE TRUE TRUE TRUE TRUE > [18] TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE TRUE TRUE > TRUE TRUE FALSE TRUE TRUE TRUE > [35] FALSE TRUE TRUE TRUE TRUE FALSE FALSE FALSE TRUE TRUE FALSE > TRUE TRUE FALSE FALSE TRUE FALSE > [52] TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE FALSE TRUE > > How can i get the values smaller than x and not the TRUE/FALSE reply? > > Thanks in advance, > Thomas > > ______________________________________________ > R-help@stat.math.ethz.ch 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. >-- Henrique Dallazuanna Curitiba-ParanĂ¡ Brasil [[alternative HTML version deleted]]
Thomas Preuth wrote:> Hello, > > I want to select in a column of a dataframe all numbers smaller than a > value x > but when I type in test<-(RSF_EU$AREA<=x) I receiv as answer: > > test > [1] TRUE FALSE FALSE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE > FALSE TRUE TRUE TRUE TRUE TRUE > [18] TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE TRUE TRUE > TRUE TRUE FALSE TRUE TRUE TRUE > [35] FALSE TRUE TRUE TRUE TRUE FALSE FALSE FALSE TRUE TRUE FALSE > TRUE TRUE FALSE FALSE TRUE FALSE > [52] TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE FALSE TRUE > > How can i get the values smaller than x and not the TRUE/FALSE reply?This is covered in http://cran.r-project.org/doc/manuals/R-intro.html (Section 2.7; Index vectors; selecting and modifying subsets of a data set) RSF_EU$AREA[RSF_EU$AREA <= x] or to retain other columns in the data frame subset(RSF_EU, AREA <= x)> Thanks in advance, > Thomas > > ______________________________________________ > R-help at stat.math.ethz.ch 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
On Fri, 2007-02-23 at 14:38 +0100, Thomas Preuth wrote:> Hello, > > I want to select in a column of a dataframe all numbers smaller than a > value x > but when I type in test<-(RSF_EU$AREA<=x) I receiv as answer: > > test > [1] TRUE FALSE FALSE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE > FALSE TRUE TRUE TRUE TRUE TRUE > [18] TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE TRUE TRUE > TRUE TRUE FALSE TRUE TRUE TRUE > [35] FALSE TRUE TRUE TRUE TRUE FALSE FALSE FALSE TRUE TRUE FALSE > TRUE TRUE FALSE FALSE TRUE FALSE > [52] TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE FALSE TRUE > > How can i get the values smaller than x and not the TRUE/FALSE reply? > > Thanks in advance, > ThomasYou need to subset your object based on the results you achieved above. What you did was only half the job. See this example, with a number of ways to get what you want: ## some dummy data to work with dat <- 10 * runif(100) dat <- data.frame(AREA = dat, FOO = dat + rnorm(100)) ## select values of AREA less than mean AREA mn <- mean(dat$AREA) want1 <- with(dat, AREA[AREA <= mn]) ## or want2 <- dat$AREA[dat$AREA <= mn] ## or want3 <- subset(dat$AREA, dat$AREA <= mn) ## or want4 <- subset(dat, AREA <= mn)$AREA ## check they all do same thing all.equal(want1, want2, want3, want4) ## TRUE want2 is closest to how you tried to do it: dat$AREA[dat$AREA <= mn] ^^^^^^^^^^^^^^ Notice that you only did the inner bit marked, which as you found returns TRUE/FALSE depending on whether that element of AREA met the criterion of being less than or equal to your x. This information is used to select elements from AREA using the subsetting functions for objects. HTH G -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
It is inefficient to use which() rather than a logical index, since you allocate two numeric index vectors (one the length of the original vector) and use an interpreted function rather than optimized C code. Also, in this usage which() handles NAs incorrectly. I think the clearest answer is probably with(RSF_EU, AREA[AREA <= x]) On Fri, 23 Feb 2007, Henrique Dallazuanna wrote:> You can also: > > test <- RSF_EU[which(RSF_EU$AREA<=x),] > > On 23/02/07, Thomas Preuth <preuth at slf.ch> wrote: >> >> Hello, >> >> I want to select in a column of a dataframe all numbers smaller than a >> value x >> but when I type in test<-(RSF_EU$AREA<=x) I receiv as answer: >>> test >> [1] TRUE FALSE FALSE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE >> FALSE TRUE TRUE TRUE TRUE TRUE >> [18] TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE TRUE TRUE >> TRUE TRUE FALSE TRUE TRUE TRUE >> [35] FALSE TRUE TRUE TRUE TRUE FALSE FALSE FALSE TRUE TRUE FALSE >> TRUE TRUE FALSE FALSE TRUE FALSE >> [52] TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE FALSE TRUE >> >> How can i get the values smaller than x and not the TRUE/FALSE reply? >> >> Thanks in advance, >> Thomas >> >> ______________________________________________ >> R-help at stat.math.ethz.ch 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. >> > > > >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595