James R. Milks
2007-Jul-08 17:08 UTC
[R] generating a data frame with a subset from another data frame
R gurus, I have a data set that looks something like this: Site Species DBH #Vines G PLOC 45.9 4 G ACNE 23.3 1 G ACNE 12.0 0 G FRAM 35.9 5 G AEGL 11.2 2 N PLOC 77.3 12 N JUNI 78.6 7 N ACNE 18.9 1 N ACNE 15.7 3 N ACRU 35.5 4 H ACSA2 24.1 6 H ULAM 35.2 7 There are 730 individual trees (22 species) from four sites in the actual data set. I would like to create a second data frame that contains just the most common species (mainly ACNE, PLOC, ULAM, FRAM, and ACSA2). Here's some of my attempts: >study.1<-subset(study,study$Species=c ("ACNE","PLOC","FRAM","ULAM","ACSA2)) Error: syntax error >study.1<-study[study$Species==,c("ACNE","PLOC","FRAM","ULAM","ACSA2)] Error: syntax error >study.1<-study[c("ACNE","PLOC","FRAM","ULAM","ACSA2"),] #This one appeared to work, but upon inspection, it just copied the entire "study" data frame instead of just copying the data I wanted, as study.1$Species had a length of 22 (the same as the original) instead of the desired length of 5. I've already consulted a book on R as well as spent the last three hours searching the R-help archives. There must be a way to get the subset I desire but it is not obvious to me. Thanks in advance for your help. Jim Milks Graduate Student Environmental Sciences Ph.D. Program Wright State University 3640 Colonel Glenn Hwy Dayton, OH 45431
jim holtman
2007-Jul-08 17:55 UTC
[R] generating a data frame with a subset from another data frame
?"%in%" I think what you want is: study.1<-subset(study,study$Species %in% c("ACNE","PLOC","FRAM","ULAM","ACSA2)) On 7/8/07, James R. Milks <james.milks at wright.edu> wrote:> R gurus, > > I have a data set that looks something like this: > > Site Species DBH #Vines > G PLOC 45.9 4 > G ACNE 23.3 1 > G ACNE 12.0 0 > G FRAM 35.9 5 > G AEGL 11.2 2 > N PLOC 77.3 12 > N JUNI 78.6 7 > N ACNE 18.9 1 > N ACNE 15.7 3 > N ACRU 35.5 4 > H ACSA2 24.1 6 > H ULAM 35.2 7 > > There are 730 individual trees (22 species) from four sites in the > actual data set. I would like to create a second data frame that > contains just the most common species (mainly ACNE, PLOC, ULAM, FRAM, > and ACSA2). Here's some of my attempts: > > >study.1<-subset(study,study$Species=c > ("ACNE","PLOC","FRAM","ULAM","ACSA2)) > Error: syntax error > > >study.1<-study[study$Species==,c("ACNE","PLOC","FRAM","ULAM","ACSA2)] > Error: syntax error > > >study.1<-study[c("ACNE","PLOC","FRAM","ULAM","ACSA2"),] > #This one appeared to work, but upon inspection, it just copied the > entire "study" data frame instead of just copying the data I wanted, > as study.1$Species had a length of 22 (the same as the original) > instead of the desired length of 5. > > I've already consulted a book on R as well as spent the last three > hours searching the R-help archives. There must be a way to get the > subset I desire but it is not obvious to me. > > Thanks in advance for your help. > > Jim Milks > > Graduate Student > Environmental Sciences Ph.D. Program > Wright State University > 3640 Colonel Glenn Hwy > Dayton, OH 45431 > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?