[R] replacing ugly for loops

andrewH ahoerner at rprogress.org
Thu Oct 11 06:10:35 CEST 2012


I have a couple of hundred American Community Survey Summary Files files
containing rectangular arrays of data, mainly though not exclusively
numeric.  Each file is referred to as a sequence (henceforth "seq").  From
these files I am trying to extract particular subsets (tables) consisting of
a sets of columns.  These tables are defined by three numbers (now in
columns in a data frame):
1.	a file identifier (seq)
2.	first column position numbers (startNo) 
3.	length of table (len)
so the columns to select for one triple would consist of
startNo:(startNo+length-1).   I am trying to create for each sequence a
vector of all the column numbers for tables in that sequence.

Obviously I could do this with nested for loops,e.g..

> seq <- c(1,1,2,2)
> startNo  <- c(3, 10, 3, 15)
> len <- c(4, 2, 5, 3)
> data.df <- data.frame(seq, startNo, len)
> 
> seq.f <- factor(data.df$seq)
> data.l <- split(data.df, seq.f)
> selectColsList<- vector("list", length(levels(seq.f)))
> for (i in seq_along(levels(seq.f))){
   selectCols <- numeric()
       for (j in seq_along(data.l[[i]]$startNo)){
           selectCols <- c(selectCols, 
data.l[[i]]$startNo[j]:(data.l[[i]]$startNo[j]
           data.l[[i]]$len[j]-1))
        }
    selectColsList[[i]] <- selectCols
}
> selectColsList
[[1]]
[1]  3  4  5  6 10 11
[[2]]
[1]  3  4  5  6  7 15 16 17

But this code strikes me as inelegant and verbose. It seems to me that there
ought to be a way to make the outer loop, (indexed with i) into a tapply
function (which is why I started with a split()), and the inner loop
(indexed with j) into some cute recursive function, but I was not able to do
so. If anyone could suggest some nicer (e.g. shorter, or faster, or just
more sophisticated) way to do this instead, I would be most grateful.

Sincerely, andrewH




--
View this message in context: http://r.789695.n4.nabble.com/replacing-ugly-for-loops-tp4645821.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list