Skip to content

Latest commit

 

History

History
24 lines (21 loc) · 563 Bytes

row_iterator.md

File metadata and controls

24 lines (21 loc) · 563 Bytes

Iterate over a set of rows in rows R

rows = function(x) lapply(seq_len(nrow(x)), function(i) lapply(x,function(c) c[i]))

I put together an improved version that is able to add an index column if desired:

rows <- function(tab, idx=TRUE) {
  # Set idx to attach an index column
   if (is.na(tab) || is.null(tab) || nrow(tab) == 0) {
      return(list())
  }
  lapply(seq_len(nrow(tab)), 
         function(i) {
           row <- unclass(tab[i,,drop=F])
           if (idx) {
             row$idx <- i
           }
           row
         })
}