[R] cycling through a long list of files and names

R. Michael Weylandt <michael.weylandt@gmail.com> michael.weylandt at gmail.com
Sat Oct 22 22:29:47 CEST 2011


The more R way to do something like this is to put all your dataframes into a list and then run

lappy(cityList, dataCleaning) # for example

To get them into a list in the first place try this

n = 1997:2011
cityList <- vector(length(n), 'list')
for (i in n){
    cityList[[i]] <- get(paste("city", i, sep="")
}

Hope this helps,

Michael


On Oct 22, 2011, at 3:13 PM, Wet Bell Diver <wetbelldiver at gmail.com> wrote:

> 
> R2.13.2, W7x64
> 
> Dear list,
> 
> Excuse my ignorance, but I have gone through the R help (?parse, ?eval, etc.) and still really don't know how to do the following.
> I have the general following structure that I would like to automate [edited to make it shorter]:
> 
> >>>
> city1997 <- dataCleaning(read.csv2("C:\\city\\year1997.txt"))
> city1997 <- wasteCalculations(city1997, year = 1997)
> if (city1997[1,1] == "Time") {city1997 <- timeCalculations(city1997)}
> city1998 <- dataCleaning(read.csv2("C:\\city\\year1998.txt"))
> city1998 <- wasteCalculations(city1998, year = 1998)
> if (city1998[1,1] == "Time") {city1998 <- timeCalculations(city1998)}
> city1999 <- dataCleaning(read.csv2("C:\\city\\year1999.txt"))
> city1999 <- wasteCalculations(city1999, year = 1999)
> if (city1999[1,1] == "Time") {city1999 <- timeCalculations(city1999)}
> 
> [....etc., all the way through....]
> 
> city2011 <- dataCleaning(read.csv2("C:\\city\\year2011.txt"))
> city2011<- wasteCalculations(city2011, year = 2011)
> if (city2011[1,1] == "Time") {city2011 <- timeCalculations(city2011)}
> 
> city.df <- data.frame(city1997$waste, city1998$waste, city1999$waste, ...,city2011$waste)
> save(city1997, city1998, city1999, ...., city2011, city.df, file = "city.Rdata")
> 
> and then the same thing with: municipality1981 through municipality2011
> and then the same thing with: county1985 through county2011
> >>>
> 
> So, for both city, municipality, and county, across a (varying) range of years the functions "dataCleaning", "wasteCalculations", and "timeCalculations" are called and the final objects are pulled together in a dataframe and are then all saved together.
> I can get all of this done manually (generating LONG repetitive code), but I have A LOT of data that needs to be processed like this and that becomes tedious and very repetitious. Besides, it feels silly to do such a task manually when using the powerful R language. Unfortunately, I have no clue how to do this. I have been wrestling with "parse", "eval", "substitute" but I have to admit that I just don't seem to really understand how they work. Anyway, I can't get this to work, but have the feeling it can be done in a few lines. Who can help me with the code and the explanation of why that code works?
> 
> Thanks,
> Peter Verbeet
> 
> ______________________________________________
> 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.



More information about the R-help mailing list