Hello, I understand that question is probably stupid, but ... I have data (polity IV index) "country","year","democ","autoc","polity","polity2" "1","Afghanistan ",1800,1,7,-6,-6 "2","Afghanistan ",1801,1,7,-6,-6 "3","Afghanistan ",1802,1,7,-6,-6 I need to create new data sets which includes only cases after year 1995. I've tried this command: poli2<-poli[poli$year > 1994,] , however it generated this: Error in poli[poli$year > 1994, ] : incorrect number of dimensions. Any ideas how i can overcome this? Thank in advance for your help, ----- Diogenas -- View this message in context: http://r.789695.n4.nabble.com/Filter-data-tp3070069p3070069.html Sent from the R help mailing list archive at Nabble.com.
> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Diogenas > Sent: Thursday, December 02, 2010 12:09 PM > To: r-help at r-project.org > Subject: [R] Filter data > > > Hello, > I understand that question is probably stupid, but ... > > I have data (polity IV index) > "country","year","democ","autoc","polity","polity2" > "1","Afghanistan ",1800,1,7,-6,-6 > "2","Afghanistan ",1801,1,7,-6,-6 > "3","Afghanistan ",1802,1,7,-6,-6 > > I need to create new data sets which includes only cases after year > 1995. > I've tried this command: > poli2<-poli[poli$year > 1994,] , > however it generated this: > Error in poli[poli$year > 1994, ] : incorrect number of dimensions. > Any ideas how i can overcome this? >We need more information. You haven't given us a self-contained, reproducible example. The syntax you show works for me. What does>str(poli)show? Dan Daniel J. Nordlund Washington State Department of Social and Health Services Planning, Performance, and Accountability Research and Data Analysis Division Olympia, WA 98504-5204
Simplified version of str(poli): (real dataset includes 36 variables) $ country : chr [1:15846] "Afghanistan " "Afghanistan " "Afghanistan " "Afghanistan " ... $ year : num [1:15846] 1800 1801 1802 1803 1804 ... $ democ : num [1:15846] 1 1 1 1 1 1 1 1 1 1 ... $ autoc : num [1:15846] 7 7 7 7 7 7 7 7 7 7 ... $ polity : num [1:15846] -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 ... $ polity2 : num [1:15846] -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 ... - attr(*, "label.table")=List of 36 ..$ country : NULL ..$ year : NULL ..$ democ : NULL ..$ autoc : NULL ..$ polity : NULL ..$ polity2 : NULL ----- Diogenas -- View this message in context: http://r.789695.n4.nabble.com/Filter-data-tp3070069p3070201.html Sent from the R help mailing list archive at Nabble.com.
Hi Diogenas, Try this, data<-subset(poli, poli$year>1994) Best, M Diogenas <diogenas at gmail.com> a ?crit?:> > Hello, > I understand that question is probably stupid, but ... > > I have data (polity IV index) > "country","year","democ","autoc","polity","polity2" > "1","Afghanistan ",1800,1,7,-6,-6 > "2","Afghanistan ",1801,1,7,-6,-6 > "3","Afghanistan ",1802,1,7,-6,-6 > > I need to create new data sets which includes only cases after year 1995. > I've tried this command: > poli2<-poli[poli$year > 1994,] , > however it generated this: > Error in poli[poli$year > 1994, ] : incorrect number of dimensions. > Any ideas how i can overcome this? > > Thank in advance for your help, > > > > > > ----- > Diogenas > -- > View this message in context: > http://r.789695.n4.nabble.com/Filter-data-tp3070069p3070069.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
Try looking at subset()... I typically use this when I'm trying to isolate specific cases from a data frame. Also, Dalgaard's introductory R book has some great examples for parsing data in the manner you're attempting.... you might want to look at that. Mike On Thu, Dec 2, 2010 at 2:08 PM, Diogenas <diogenas@gmail.com> wrote:> > Hello, > I understand that question is probably stupid, but ... > > I have data (polity IV index) > "country","year","democ","autoc","polity","polity2" > "1","Afghanistan ",1800,1,7,-6,-6 > "2","Afghanistan ",1801,1,7,-6,-6 > "3","Afghanistan ",1802,1,7,-6,-6 > > I need to create new data sets which includes only cases after year 1995. > I've tried this command: > poli2<-poli[poli$year > 1994,] , > however it generated this: > Error in poli[poli$year > 1994, ] : incorrect number of dimensions. > Any ideas how i can overcome this? > > Thank in advance for your help, > > > > > > ----- > Diogenas > -- > View this message in context: > http://r.789695.n4.nabble.com/Filter-data-tp3070069p3070069.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Michael Rennie, Research Scientist Fisheries and Oceans Canada, Freshwater Institute Winnipeg, Manitoba, CANADA [[alternative HTML version deleted]]
Hi Hm. Does str(poli) revealed said explicitly that poli is data frame? I presume that it told you that it is list. In that case you need to use appropriate indexing, which suits structure of your list. Regards r-help-bounces at r-project.org napsal dne 02.12.2010 22:06:15:> > Simplified version of str(poli): (real dataset includes 36 variables) > > $ country : chr [1:15846] "Afghanistan " "Afghanistan > " "Afghanistan " "Afghanistan " ... > $ year : num [1:15846] 1800 1801 1802 1803 1804 ... > $ democ : num [1:15846] 1 1 1 1 1 1 1 1 1 1 ... > $ autoc : num [1:15846] 7 7 7 7 7 7 7 7 7 7 ... > $ polity : num [1:15846] -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 ... > $ polity2 : num [1:15846] -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 ... > - attr(*, "label.table")=List of 36 > ..$ country : NULL > ..$ year : NULL > ..$ democ : NULL > ..$ autoc : NULL > ..$ polity : NULL > ..$ polity2 : NULL > > > > > ----- > Diogenas > -- > View this message in context: http://r.789695.n4.nabble.com/Filter-data- > tp3070069p3070201.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.