Hi, I am trying to find the total number of rows for a list of data.frames and want to know if there is a better way than using a loop like:>df = { list of data.frame with varying number of rows...each one has acolumn called "COL" }>r = 0 > for (i in 1:length(df)) {+ r = r + length(n[[i]]$CON) + }> r6000123 <---- number of rows. Thanks, Chris -- View this message in context: http://r.789695.n4.nabble.com/SApply-versus-for-loop-for-list-of-data-frames-tp2991107p2991107.html Sent from the R help mailing list archive at Nabble.com.
On Oct 12, 2010, at 12:16 AM, rivercode wrote:> > Hi, > > I am trying to find the total number of rows for a list of > data.frames and > want to know if there is a better way than using a loop like: > >> df = { list of data.frame with varying number of rows...each one >> has a > column called "COL" } >> r = 0 >> for (i in 1:length(df)) { > + r = r + length(n[[i]]$CON) > + } >> r > 6000123 <---- number of rows.r <- lapply(df, NROW) r -- David.
On Oct 12, 2010, at 12:33 AM, David Winsemius wrote:> > On Oct 12, 2010, at 12:16 AM, rivercode wrote: > >> >> Hi, >> >> I am trying to find the total number of rows for a list of >> data.frames and >> want to know if there is a better way than using a loop like: >> >>> df = { list of data.frame with varying number of rows...each one >>> has a >> column called "COL" } >>> r = 0 >>> for (i in 1:length(df)) { >> + r = r + length(n[[i]]$CON) >> + } >>> r >> 6000123 <---- number of rows. > > > r <- lapply(df, NROW) > rRather: sum(unlist(r)) # or sum(sapply(df,NROW)) sum> > -- > David.