Hello R list. I am sure this is an easy question, please forgive my ignorance of R. I have a panel data set (25 years) and I would like to break it up by year, and run a few tests. How is this done in R? In STATA it is quite simple (keep if year =1970). If someone could give me an example of how to do it for a single year I am sure I could make a little loop to do it for all 25. Thanks, JGG.
"Justin G. Gardner" <jggardne at uiuc.edu> writes:> Hello R list. > > I am sure this is an easy question, please forgive my > ignorance of R. > > > I have a panel data set (25 years) and I would like to break > it up by year, and run a few tests. How is this done in R? > In STATA it is quite simple (keep if year =1970). If someone > could give me an example of how to do it for a single year I > am sure I could make a little loop to do it for all 25.There are multiple answers, mostly involving the subset(), tapply(), or by() functions. So use help(subset), etc. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
"by" is perhaps what you looking for try to write "?by" for help>I have a panel data set (25 years) and I would like to break >it up by year, and run a few tests. How is this done in R? >In STATA it is quite simple (keep if year =1970). If someone >could give me an example of how to do it for a single year I >am sure I could make a little loop to do it for all 25.Otto
You probably want something like subset(dataset, year == 1970) and loop through the years. Or, perhaps even classier, would be datalist <- split(dataset, as.factor(year)) results <- lapply(datalist, myAnalysisFunction) -roger Justin G. Gardner wrote:> Hello R list. > > I am sure this is an easy question, please forgive my > ignorance of R. > > > I have a panel data set (25 years) and I would like to break > it up by year, and run a few tests. How is this done in R? > In STATA it is quite simple (keep if year =1970). If someone > could give me an example of how to do it for a single year I > am sure I could make a little loop to do it for all 25. > > Thanks, > JGG. > > ______________________________________________ > 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 >