Hello everybody, I have usually solved this problem by repeating lines of codes instead of a loop, but it's such a waste of time, I thought I should really learn how to do it with loops: What I want to do: Say, I have several data files that differ only in a number, e.g. data points (or vector, or matrix...) Data_1, Data_2, Data_3,... and I want to manipulate them e.g. a simple sum of several data points>data <- c(NA,n) >for (i in 1:n){ >data[i] <- Data_i + Data_[i-1] > }I know that the above code doesn't work, and I don't want to combine the files into one vector to solve the problem etc. - I would just like to know who make sure R recognizes the extension "_i". I have the same problem for say, reading in datafiles that only differ by one digit in the extension, and I want to (instead of repeating code) combine the process in a loop. I hope I made myself clear to what my problem is. Thanks for your help, //F -- View this message in context: http://r.789695.n4.nabble.com/Using-changing-names-in-loop-in-R-tp3030132p3030132.html Sent from the R help mailing list archive at Nabble.com.
> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] > On Behalf Of Tuatara > Sent: Saturday, November 06, 2010 9:22 AM > To: r-help at r-project.org > Subject: [R] Using changing names in loop in R > > > Hello everybody, > > I have usually solved this problem by repeating lines of codes instead of > a > loop, but it's such a waste of time, I thought I should really learn how > to > do it with loops: > > What I want to do: > > Say, I have several data files that differ only in a number, e.g. data > points (or vector, or matrix...) Data_1, Data_2, Data_3,... and I want to > manipulate them > > e.g. a simple sum of several data points > > >data <- c(NA,n) > >for (i in 1:n){ > >data[i] <- Data_i + Data_[i-1] > > } > > I know that the above code doesn't work, and I don't want to combine the > files into one vector to solve the problem etc. - I would just like to > know > who make sure R recognizes the extension "_i". I have the same problem for > say, reading in datafiles that only differ by one digit in the extension, > and I want to (instead of repeating code) combine the process in a loop. > > I hope I made myself clear to what my problem is. > > Thanks for your help, >This is one of those cases where a commented, self-contained, reproducible example would be very helpful in helping you. You mention you have several "data files", but I see no reference to data files in your code. Did you mean data frames? What is Data_i? A data frame or something else? You said you normally do this by repeating lines of code. Can you show us a simple example? Someone should be able to show you how to optimize it. Dan Daniel Nordlund Bothell, WA USA
On Sat, Nov 6, 2010 at 5:22 PM, Tuatara <franziskabroell at gmail.com> wrote:> > Hello everybody, > > I have usually solved this problem by repeating lines of codes instead of a > loop, but it's such a waste of time, I thought I should really learn how to > do it with loops: >Would the following construct help?> for(i in 1:10) assign(paste('x', i, sep=''), c(i:10)) > ls()[1] "i" "pkg" "tbbt" "x1" "x10" "x2" "x3" "x4" "x5" "x6" [11] "x7" "x8" "x9"> for(i in 1:10) print(get(paste('x', i, sep='')))[1] 1 2 3 4 5 6 7 8 9 10 [1] 2 3 4 5 6 7 8 9 10 [1] 3 4 5 6 7 8 9 10 [1] 4 5 6 7 8 9 10 [1] 5 6 7 8 9 10 [1] 6 7 8 9 10 [1] 7 8 9 10 [1] 8 9 10 [1] 9 10 [1] 10 Read ?assign, ?get, but also this fortune:> fortune('assign')The only people who should use the assign function are those who fully understand why you should never use the assign function. -- Greg Snow R-help (July 2009) I haven't yet figured out why I should heed this. Regards Liviu> What I want to do: > > Say, I have several data files that differ only in a number, e.g. data > points (or vector, or matrix...) Data_1, Data_2, Data_3,... and I want to > manipulate them > > e.g. a simple sum of several data points > >>data <- c(NA,n) >>for (i in 1:n){ >>data[i] <- Data_i + Data_[i-1] >> ? ? ? ? ? ? ? ? ?} > > I know that the above code doesn't work, and I don't want to combine the > files into one vector to solve the problem etc. - I would just like to know > who make sure R recognizes the extension "_i". I have the same problem for > say, reading in datafiles that only differ by one digit in the extension, > and I want to (instead of repeating code) combine the process in a loop. > > I hope I made myself clear to what my problem is. > > Thanks for your help, > > //F > -- > View this message in context: http://r.789695.n4.nabble.com/Using-changing-names-in-loop-in-R-tp3030132p3030132.html > Sent from the R help mailing list archive at Nabble.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. >-- Do you know how to read? http://www.alienetworks.com/srtest.cfm http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader Do you know how to write? http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail
A more detailed example: Say I would like to read in data files that are set-up identically and have identical (but somewhat) different text names (see below): data_1 <- read.csv("data1.txt") data_2 <- read.csv("data2.txt") data_3 <- read.csv("data3.txt") How do I automate this process? (I assume the way I make R understand that the data file extension is to be read as a number rather than a string is the same for things like applying functions to matrices with different extensions, e.g. data_i, i = 1,2,3) -- View this message in context: http://r.789695.n4.nabble.com/Using-changing-names-in-loop-in-R-tp3030132p3030412.html Sent from the R help mailing list archive at Nabble.com.
Hi, If you have data that is similar enough to warrant only changing the extension (i.e., 1, 2, etc.) and that you (at least at times) wish to perform operations on together, it is time to start thinking of a more flexible framework. Fortunately, such a framework already exists in lists. Lists let you keep diverse data structures (i.e., you do not have to combine everything into a simple vector). Lists make it tremendously easy to do many tasks (including the two you mentioned). For example, suppose I want to read in 10 files and then do some manipulations: ## initialize an empty list dat <- vector(mode = "list", length = 10) for(i in 1:10) { dat[[i]] <- read.table(paste(myfilename, i, sep = ''), header = TRUE, etc.) } # presumably these are data frames at this point Now everything is nicely stored, dat[[1]] is even intuitively similar to Data_1, Data_2, etc. At this point, suppose I want to create some new stuff: dat[[11]] <- matrix(1, ncol = 1, nrow = 5) # add in a matrix for(i in 1:n) { dat[[i]] <- dat[[i]] + dat[[i - 1]] } dat[[12]] <- 5 # just a little vector Another great advantage of lists is that it is possible to name their elements. This can make things more meaningful or aid the memory. However, even when an element is named, it can still be accessed by its index # name the first element 'price' names(dat)[[1]] <- "price" now I could access it as any of these: dat$price dat[["price"]] dat[[1]] If that weren't enough, you can easily use functions on every element of a list with constructs such as lapply(), no for loop required! lapply(X = dat, FUN = mean, na.rm = TRUE) It is possible to not use lists and still do what you are after, but frankly it is messier, more prone to error, and less effective in many cases. It is generally a very nice feature of the assignment operator that it is aware of its environment and does not go assigning or overwriting things where you do not expect. You're left to the wolves and your own wits with assign(). HTH, Josh On Sat, Nov 6, 2010 at 9:22 AM, Tuatara <franziskabroell at gmail.com> wrote:> > Hello everybody, > > I have usually solved this problem by repeating lines of codes instead of a > loop, but it's such a waste of time, I thought I should really learn how to > do it with loops: > > What I want to do: > > Say, I have several data files that differ only in a number, e.g. data > points (or vector, or matrix...) Data_1, Data_2, Data_3,... and I want to > manipulate them > > e.g. a simple sum of several data points > >>data <- c(NA,n) >>for (i in 1:n){ >>data[i] <- Data_i + Data_[i-1] >> ? ? ? ? ? ? ? ? ?} > > I know that the above code doesn't work, and I don't want to combine the > files into one vector to solve the problem etc. - I would just like to know > who make sure R recognizes the extension "_i". I have the same problem for > say, reading in datafiles that only differ by one digit in the extension, > and I want to (instead of repeating code) combine the process in a loop. > > I hope I made myself clear to what my problem is. > > Thanks for your help, > > //F > -- > View this message in context: http://r.789695.n4.nabble.com/Using-changing-names-in-loop-in-R-tp3030132p3030132.html > Sent from the R help mailing list archive at Nabble.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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/