If you have 5 data frames and you append them to a list, how do you access the first data frame, not the first value of the first data frame while iterating in a for loop? list = c(d1,d2,d3,d4,d5) where d1..d5 are dataframes. for(i in 1: length(list)){ print(list[1]) } -- View this message in context: http://r.789695.n4.nabble.com/extracting-objects-from-lists-tp2539412p2539412.html Sent from the R help mailing list archive at Nabble.com.
First create a list of the dataframe and then do the loop: myList <- list(d1, d2, d3, d4, d5) for (i in myList) print(i) or for (i in seq(length(myList))) print(myList[[i]]) On Tue, Sep 14, 2010 at 2:13 PM, lord12 <gaut222 at yahoo.com> wrote:> > If you have 5 data frames and you append them to a list, how do you access > the first data frame, not the first value of the first data frame while > iterating in a for loop? > > list = c(d1,d2,d3,d4,d5) where d1..d5 are dataframes. > for(i in 1: length(list)){ > print(list[1]) > } > -- > View this message in context: http://r.789695.n4.nabble.com/extracting-objects-from-lists-tp2539412p2539412.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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Hi: On Tue, Sep 14, 2010 at 11:13 AM, lord12 <gaut222@yahoo.com> wrote:> > If you have 5 data frames and you append them to a list, how do you access > the first data frame, not the first value of the first data frame while > iterating in a for loop? >For a list named l, l[[1]] accesses the first component, or if you have it named df1, say, then l$df1.> list = c(d1,d2,d3,d4,d5) where d1..d5 are dataframes. > for(i in 1: length(list)){ > print(list[1]) > } >You wouldn't do it that way. Instead, l <- list(d1 = d1, d2 = d2, d3 = d3, d4 = d4, d5 =d5) # a named list l will print each component one after the other, or l$d1 will print the first component. BTW, it's not good form to name your objects after commonly used functions. Strange things can happen... Welcome to the wonderful world of subscripting in R :) See ?Extract for a brief, concise description. HTH, Dennis --> View this message in context: > http://r.789695.n4.nabble.com/extracting-objects-from-lists-tp2539412p2539412.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
You can explore the structure of the list with str(). If you use Eclipse, a list of available options will be created for you as you type (see http://imagepaste.nullnetwork.net/viewimage.php?id=1293). Cheers, Roman -- View this message in context: http://r.789695.n4.nabble.com/extracting-objects-from-lists-tp2539412p2540051.html Sent from the R help mailing list archive at Nabble.com.