Dear R users; I am trying to write a small program which reads in a data set, and selects observations from certain years before the analysis. I have a problem including the selection criteria in the header of the program. Here is the problem; dataFIT<-function(MODEL, MARGINS, yearConsidered){ library(foreign CovPaper<-read.spss("C:/Data/data.sav") NewData <- list(CovPaper$"YEAR"[CovPaper$"YEAR" == yearConsidered], CovPaper$"YEAR"[CovPaper$"SEX" == yearConsidered], #### and so on ##### #fit model to data # } When I use one year there is no problem, but I would like some data sets to span over years and I am not sure how to do this without having to change the body of my fitting program. I tried searching the R-list but to no avail. Thanking you in advance. Vumani _________________________________________________________________
Thanks man. It did the trick. Vumani>From: Winfried Theis <theis at statistik.uni-dortmund.de> >To: Vumani Dlamini <dvumani at hotmail.com> >CC: r-help at stat.math.ethz.ch >Subject: Re: [R] subsetting based on vector >Date: Thu, 25 Mar 2004 16:16:58 +0100 > >Hi! > >Well, you could try a subsetting rule like " year %in% yearsconsidered >". > >Hope this helps, > >Winfried >On Thu, 2004-03-25 at 14:33, Vumani Dlamini wrote: > > Dear R users; > > > > I am trying to write a small program which reads in a data set, and >selects > > observations from certain years before the analysis. I have a problem > > including the selection criteria in the header of the program. > > > > Here is the problem; > > > > dataFIT<-function(MODEL, MARGINS, yearConsidered){ > > library(foreign > > CovPaper<-read.spss("C:/Data/data.sav") > > NewData <- list(CovPaper$"YEAR"[CovPaper$"YEAR" == yearConsidered], > > CovPaper$"YEAR"[CovPaper$"SEX" == >yearConsidered], > > #### and so on ##### > > #fit model to data # > > } > > > > When I use one year there is no problem, but I would like some data sets >to > > span over years and I am not sure how to do this without having to >change > > the body of my fitting program. > > > > I tried searching the R-list but to no avail. > > > > Thanking you in advance. > > > > > > Vumani > > > > _________________________________________________________________ > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide! >http://www.R-project.org/posting-guide.html >-- >----------------------------------------------------------------- >Dr. Dipl.-Math. Winfried Theis, SFB 475, Projekt C5, >Universität Dortmund, 44221 Dortmund >e-mail: theis at statistik.uni-dortmund.de >Tel.: +49/231/755-5903 FAX: +49/231/755-4387 >
Hi! Well, you could try a subsetting rule like " year %in% yearsconsidered ". Hope this helps, Winfried On Thu, 2004-03-25 at 14:33, Vumani Dlamini wrote:> Dear R users; > > I am trying to write a small program which reads in a data set, and selects > observations from certain years before the analysis. I have a problem > including the selection criteria in the header of the program. > > Here is the problem; > > dataFIT<-function(MODEL, MARGINS, yearConsidered){ > library(foreign > CovPaper<-read.spss("C:/Data/data.sav") > NewData <- list(CovPaper$"YEAR"[CovPaper$"YEAR" == yearConsidered], > CovPaper$"YEAR"[CovPaper$"SEX" == yearConsidered], > #### and so on ##### > #fit model to data # > } > > When I use one year there is no problem, but I would like some data sets to > span over years and I am not sure how to do this without having to change > the body of my fitting program. > > I tried searching the R-list but to no avail. > > Thanking you in advance. > > > Vumani > > _________________________________________________________________ > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html-- ----------------------------------------------------------------- Dr. Dipl.-Math. Winfried Theis, SFB 475, Projekt C5, Universit?t Dortmund, 44221 Dortmund e-mail: theis at statistik.uni-dortmund.de Tel.: +49/231/755-5903 FAX: +49/231/755-4387
Since you are interested in subsetting, a little search of the online help such as help.search('subset') will reveal the subset() function. Which you could use, for example, like this: dataFIT<-function(MODEL, MARGINS, yearConsidered){ library(foreign CovPaper<-read.spss("C:/Data/data.sav") NewData <- subset(CovPaper, YEAR %in% yearConsidered) #fit model to data # } If you truly need NewData to be a list, use as.list(). Also, within the context of your example, you don't need all the quotes; you can use CovPaper$YEAR instead of CovPaper$"YEAR". Also check the documentation of the function(s) you are using to fit the model(s) for a subset argument; that might be sufficient for your needs. -Don At 1:33 PM +0000 3/25/04, Vumani Dlamini wrote:>Dear R users; > >I am trying to write a small program which reads in a data set, and >selects observations from certain years before the analysis. I have >a problem including the selection criteria in the header of the >program. > >Here is the problem; > >dataFIT<-function(MODEL, MARGINS, yearConsidered){ > library(foreign > CovPaper<-read.spss("C:/Data/data.sav") > NewData <- list(CovPaper$"YEAR"[CovPaper$"YEAR" == yearConsidered], > CovPaper$"YEAR"[CovPaper$"SEX" == yearConsidered], > #### and so on ##### > #fit model to data # >} > >When I use one year there is no problem, but I would like some data >sets to span over years and I am not sure how to do this without >having to change the body of my fitting program. > >I tried searching the R-list but to no avail. > >Thanking you in advance. > > >Vumani > >_________________________________________________________________ > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA