Hi Need a little help. This is easy question, but i?m new to R. I want to subset a data frame called vef1 using a variable that is a factor called edad11, 0 if under 11 years, 1 otherwise. I tried: vef1.sub<-subset(vef1, edad11==0) The code runs correctly but i get a empty dataframe. If i use this code in Deducer everything is OK. Can you help me? Thanks in advance Antonio Sousa Portugal
Hmmm.... a trivial mockup suggests this should work: x <- factor(sample(c(0, 1), 30, TRUE) x == 0 so perhaps you could work up a reproducible example for us: see this site for more details on how to do so: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example Best, Michael On Thu, Aug 2, 2012 at 1:15 PM, antonio.bsousa at sapo.pt <antonio.bsousa at sapo.pt> wrote:> Hi > Need a little help. This is easy question, but i?m new to R. > I want to subset a data frame called vef1 using a variable that is a factor called edad11, 0 if under 11 years, 1 otherwise. > I tried: > vef1.sub<-subset(vef1, edad11==0) > The code runs correctly but i get a empty dataframe. > If i use this code in Deducer everything is OK. > Can you help me? > Thanks in advance > Antonio Sousa > Portugal > > ______________________________________________ > 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, You need to use, subset(vef1,vef1$edad1==0) vef1<-read.table(text=" age? edad1 12???? 1 10???? 0 9????? 0 14???? 1 15???? 1 ",sep="",header=TRUE)???? subset(vef1,vef1$edad1==0) ? age edad1 2? 10???? 0 3?? 9???? 0 A.K. ----- Original Message ----- From: "antonio.bsousa at sapo.pt" <antonio.bsousa at sapo.pt> To: r-help at r-project.org Cc: Sent: Thursday, August 2, 2012 2:15 PM Subject: [R] Subset data Hi Need a little help. This is easy question, but i?m new to R. I want to subset a data frame called vef1 using a variable that is a factor called edad11, 0 if under 11 years, 1 otherwise. I tried: vef1.sub<-subset(vef1, edad11==0) The code runs correctly but i get a empty dataframe. If i use this code in Deducer everything is OK. Can you help me? Thanks in advance Antonio Sousa Portugal ______________________________________________ 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 Antonio, It's hard to know for sure what the problem is, because we don't know what your vef1 data.frame contains. My guess is that edad11 is not numeric, so never equals 0 (although it might equal the character string "0"). If you paste the results of str(vef1) in your reply we can see if my guess is correct. Best, Ista On Thu, Aug 2, 2012 at 2:15 PM, antonio.bsousa at sapo.pt <antonio.bsousa at sapo.pt> wrote:> Hi > Need a little help. This is easy question, but i?m new to R. > I want to subset a data frame called vef1 using a variable that is a factor called edad11, 0 if under 11 years, 1 otherwise. > I tried: > vef1.sub<-subset(vef1, edad11==0) > The code runs correctly but i get a empty dataframe. > If i use this code in Deducer everything is OK. > Can you help me? > Thanks in advance > Antonio Sousa > Portugal > > ______________________________________________ > 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.