cycling through a long list of files and names
You might also need the assign() function which is sort of the opposite of get() Michael
On Mon, Oct 24, 2011 at 12:15 PM, jim holtman <jholtman at gmail.com> wrote:
Write a function that encapsulates the following three lines:
city1997<- dataCleaning(read.csv2("C:\\city\\year1997.txt"))
city1997<- wasteCalculations(city1997, year = 1997)
if (city1997[1,1] == "Time") {city1997<- timeCalculations(city1997)}
and then pass in the appropriate parameters.
On Mon, Oct 24, 2011 at 12:09 PM, Wet Bell Diver <wetbelldiver at gmail.com> wrote:
Thanks so much, this is very very helpful.
I do have one remaining question here. I definitely see the value of making
a list of the datasets, an advise I will definitely follow. However, for
educational purposes, I would still like to know how to automate the
following without using a list:
city1997<- dataCleaning(read.csv2("C:\\city\\year1997.txt"))
city1997.waste<- wasteCalculations(city1997, year = 1997)
if (city1997.waste[1,1] == "Time") {city1997.time<-
timeCalculations(city1997)}
city1998<- dataCleaning(read.csv2("C:\\city\\year1998.txt"))
city1998.waste<- wasteCalculations(city1998, year = 1998)
if (city1998.waste[1,1] == "Time") {city1998.time<-
timeCalculations(city1998)}
city1999<- dataCleaning(read.csv2("C:\\city\\year1999.txt"))
city1999.waste<- wasteCalculations(city1999, year = 1999)
if (city1999.waste[1,1] == "Time") {city1999.time<-
timeCalculations(city1999)}
save(city1997, city1998, city1999, city1997.waste, city1998.waste,
city1999.waste, city1997.time, city1998.time, city1999.time, file =
"cities.Rdata")
so, how do I create objects with appropriate names and then have functions
applied to them. (this is only an example of the kinds of manipulations I
need to do, but if I can get the above to work, then I can figure out the
rest for myself).
Thanks for your help, can you solve this final piece of the puzzle as well?
--Peter
Op 23-10-2011 3:51, R. Michael Weylandt schreef:
I had no idea mget() existed. How helpful! Thanks, MW On Sat, Oct 22, 2011 at 9:27 PM, Joshua Wiley<jwiley.psych at gmail.com> ?wrote:
Or simplify things down:
cityList<- mget(paste("city", 1997:2011, sep = ''), envir = .GlobalEnv)
mget returns a list, all in one step.
Cheers,
Josh
On Sat, Oct 22, 2011 at 6:19 PM, R. Michael Weylandt
<michael.weylandt at gmail.com> ?wrote:
A small clarification: the correct syntax would have been
vector("list", length(n))
Michael
On Sat, Oct 22, 2011 at 4:29 PM, R. Michael Weylandt
<michael.weylandt at gmail.com> ?<michael.weylandt at gmail.com> ?wrote:
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.
______________________________________________ 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.
-- Joshua Wiley Ph.D. Student, Health Psychology Programmer Analyst II, ATS Statistical Consulting Group University of California, Los Angeles https://joshuawiley.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.
-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?
______________________________________________ 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.