I have a data frame frugivore.abundance.S1 where some columns are factors and others are numbers.For example these are my independent variables and "density" is my dependent variable. census<-c(1:70)sites<-c(1:5)birds<-c(1:45) I want to select the data where sites is 1 and birds are 1,23,24 or 29 So I write:fa1<-frugivore.abundance.S1attach(fa1)(abund.frug.RN1<-fa1[sites==1 & birds==c(1,23,24,29),]) This code doesn't print all the data it should for some reason. It seems to not print rows where "density" has the same value as another row with the same criteria. i.e. if in the original data we have the following then only rows 1 and 3 will be printed, not all of them: census sites birds density1 1 1 0.0032 1 1 0.0033 1 1 0.001 Can anyone help me out with this please? RegardsMariki [[alternative HTML version deleted]]
> where ... birds are 1,23,24 or 29 ... > birds==c(1,23,24,29)Use is.element(birds, c(1,23,24,29)) or (birds %in% c(1,23,24,29)) if you prefer typing percent signs. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of Mariki Zietsman > Sent: Friday, September 27, 2013 11:20 AM > To: r-help at r-project.org > Subject: [R] Logical indexing not working > > I have a data frame frugivore.abundance.S1 where some columns are factors and others > are numbers.For example these are my independent variables and "density" is my > dependent variable. census<-c(1:70)sites<-c(1:5)birds<-c(1:45) > > I want to select the data where sites is 1 and birds are 1,23,24 or 29 > So I write:fa1<-frugivore.abundance.S1attach(fa1)(abund.frug.RN1<-fa1[sites==1 & > birds==c(1,23,24,29),]) > This code doesn't print all the data it should for some reason. It seems to not print rows > where "density" has the same value as another row with the same criteria. > i.e. if in the original data we have the following then only rows 1 and 3 will be printed, > not all of them: > census sites birds density1 1 1 0.0032 1 1 0.0033 > 1 1 0.001 > Can anyone help me out with this please? > RegardsMariki > > [[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.
Hello, Please don't post in HTML, it messes up the code and data. And please use ?dput to post your data: dput(fa1) # post the output of this. As for your problem, maybe if you try ?%in% fa1[sites==1 & birds %in% c(1,23,24,29),] Hope this helps, Rui Barradas Em 27-09-2013 19:20, Mariki Zietsman escreveu:> I have a data frame frugivore.abundance.S1 where some columns are factors and others are numbers.For example these are my independent variables and "density" is my dependent variable. census<-c(1:70)sites<-c(1:5)birds<-c(1:45) > > I want to select the data where sites is 1 and birds are 1,23,24 or 29 > So I write:fa1<-frugivore.abundance.S1attach(fa1)(abund.frug.RN1<-fa1[sites==1 & birds==c(1,23,24,29),]) > This code doesn't print all the data it should for some reason. It seems to not print rows where "density" has the same value as another row with the same criteria. > i.e. if in the original data we have the following then only rows 1 and 3 will be printed, not all of them: > census sites birds density1 1 1 0.0032 1 1 0.0033 1 1 0.001 > Can anyone help me out with this please? > RegardsMariki > > [[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. >
Your post demonstrates why we ask people to NOT post in HTML. Attempting to decode it, I think the problem might be birds==c(1,23,24,29) Look at this: birds <- c(2, 4, 1, 23, 8, 24) birds <- data.frame(birds) birds[birds[,1] == c(1,23,24,29),] And compare birds[birds[,1] %in% c(1,23,24,29),] You might also benefit from reading ?subset and the Introduction to R that came with your installation. Sarah On Fri, Sep 27, 2013 at 2:20 PM, Mariki Zietsman <morgandetoi at hotmail.com> wrote:> I have a data frame frugivore.abundance.S1 where some columns are factors and others are numbers.For example these are my independent variables and "density" is my dependent variable. census<-c(1:70)sites<-c(1:5)birds<-c(1:45) > > I want to select the data where sites is 1 and birds are 1,23,24 or 29 > So I write:fa1<-frugivore.abundance.S1attach(fa1)(abund.frug.RN1<-fa1[sites==1 & birds==c(1,23,24,29),]) > This code doesn't print all the data it should for some reason. It seems to not print rows where "density" has the same value as another row with the same criteria. > i.e. if in the original data we have the following then only rows 1 and 3 will be printed, not all of them: > census sites birds density1 1 1 0.0032 1 1 0.0033 1 1 0.001 > Can anyone help me out with this please? > RegardsMariki >-- Sarah Goslee http://www.functionaldiversity.org
This is Circle 8.1.6 of 'The R Inferno'. http://www.burns-stat.com/documents/books/the-r-inferno/ Pat On 27/09/2013 19:20, Mariki Zietsman wrote:> I have a data frame frugivore.abundance.S1 where some columns are factors and others are numbers.For example these are my independent variables and "density" is my dependent variable. census<-c(1:70)sites<-c(1:5)birds<-c(1:45) > > I want to select the data where sites is 1 and birds are 1,23,24 or 29 > So I write:fa1<-frugivore.abundance.S1attach(fa1)(abund.frug.RN1<-fa1[sites==1 & birds==c(1,23,24,29),]) > This code doesn't print all the data it should for some reason. It seems to not print rows where "density" has the same value as another row with the same criteria. > i.e. if in the original data we have the following then only rows 1 and 3 will be printed, not all of them: > census sites birds density1 1 1 0.0032 1 1 0.0033 1 1 0.001 > Can anyone help me out with this please? > RegardsMariki > > [[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. >-- Patrick Burns pburns at pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming')