I have created a script that analyzes a set of raw data and converts it into many different formats based on different parameters and functions. I have 152 more raw data sheets to go, but all I will have to do is use my script on each one. However, there will be times that I might decide I need to change a variable or parameter and I would like to come up with a parameter list at the top of my spreadsheet that would affect the rest of the functions in my soon to be very large script.
-
Global variables aren’t the answer to this problem, this is best illustrated through this example:
exceedes <- function (L=NULL, R=NULL) { if (is.null(L) | is.null(R)) { print ("mycols: invalid L,R.") return (NULL) } options (na.rm = TRUE) test <-(mean(L, na.rm=TRUE)-R*sd(L,na.rm=TRUE)) test1 <- ifelse(is.na(L), NA, ifelse(L > test, 1, 0)) return (test1) } L=ROCC[,2] R=.08 ROCC$newcolumn <- exceedes(L,R) names(ROCC)[names(ROCC)=="newcolumn"]="Exceedes1" L=ROCC[,2] R=.16 ROCC$newcolumn <- exceedes(L,R) names(ROCC)[names(ROCC)=="newcolumn"]="Exceedes2" L=ROCC[,2] R=.24 ROCC$newcolumn <- exceedes(L,R) names(ROCC)[names(ROCC)=="newcolumn"]="Exceedes3"
So in the above example, I would like to have a way at the top of my script to change the range of R and have it affect the rest of the script because this function will be repeated 152 times. The only way I can think of doing it is to copy and paste the function over and over with a different variable each time, and set it globally. But I have to imagine there is a simpler way, my function possibly needs to be rearranged perhaps?
-
File names and output names. I am not sure whether this is possible but say for example that all my input.csv’s come in a format where one dataset will be titled 123 another will be 124, another 125 etc. and then have R know to take the very next dataset, and then output that dataset to a specific folder on my computer without me having to actually type in read.csv(file=”123.csv”), and then write.csv(example, file=”123.csv) and so on?
-
General formatting of automation script
Before I dive into my automation, my procedure was going to be to copy and past the script 152 times over and then change the filename and output name for each one. This sounds ridiculous, but with my lack of programming skills I am not sure a better way to change it. Any ideas?
Thanks for all the help in advance.
You can rerun the function with different parameters by constructing a vector of paremters (say
R)and then run your
exceedesfunctionlength(R)times usingsapply.You can pass other arguments to your function (e.g.
file.name) and use it to create whatever file name you need.