mam3xs
2010-May-12 02:50 UTC
[R] Is there an easier way to replace the object name in the R code
Hi all, I have a R code with quite a few lines. For instance AA <- read.csv(C:\\AA.txt) AA.diff <- diff(log(AA)) ..... .... Now I want to re-define the code with different object, like BB, CC......ZZ(the codes are the same). Thus, I was wondering whether their is an efficient way or automatic way to replace and generate new objects rather than using "find and replace" function in txt/word manually....? Appreciate for any help. Many thanks mam [[alternative HTML version deleted]]
Erik Iverson
2010-May-12 04:18 UTC
[R] Is there an easier way to replace the object name in the R code
mam3xs wrote:> Hi all, > > I have a R code with quite a few lines. For instance > > AA <- read.csv(C:\\AA.txt) > AA.diff <- diff(log(AA)) > ..... > .... > > Now I want to re-define the code with different object, like BB, > CC......ZZ(the codes are the same). Thus, I was wondering whether their is > an efficient way or automatic way to replace and generate new objects rather > than using "find and replace" function in txt/word manually....? >Defining your own functions, using lists, and lapply would work just great most likely. Untested, but something like should work: #create list of all file names files <- sapply(LETTERS, function(x) paste(x,x, sep = "", collapse = "")) #read in the files into a list. my.list <- lapply(files, function(x) read.csv(paste("C:\\", x, ".txt", sep = "")) #apply a function to each item in the list lapply(my.list, function(x) diff(log(x)))
Apparently Analagous Threads
- replace values in vector from a replacement table
- divide column in a dataframe based on a character
- Subsetting a zooreg object using window / subset
- Unlisting while preserving object types?
- Reading a file line by line - separating lines VS separating columns