I have what I'm sure will turn out to be straightforward. I want to store the results of a loop for some operations from a patterned vector. For example, the following doesn't give what I would hope for ss <- c(2,3,9) results <- numeric(length(ss)) for (i in seq(along=ss)){ results[i] <- i + 1 } The following does give what I expect, but creates a vector of length 9. ss <- c(2,3,9) results <- numeric(length(ss)) for (i in ss){ results[i] <- i + 1 } What I am hoping for is that results should be a vector of length 3. Harold [[alternative HTML version deleted]]
On Mon, 24 Apr 2006, Doran, Harold wrote:> I have what I'm sure will turn out to be straightforward. I want to > store the results of a loop for some operations from a patterned vector. > For example, the following doesn't give what I would hope for > > ss <- c(2,3,9) > results <- numeric(length(ss)) > for (i in seq(along=ss)){ > results[i] <- i + 1 > } > > The following does give what I expect, but creates a vector of length 9. > > ss <- c(2,3,9) > results <- numeric(length(ss)) > for (i in ss){ > results[i] <- i + 1 > } > > What I am hoping for is that results should be a vector of length 3. >either results<-sapply(ss, function(i) i+1) or for(i in seq(along=ss)){ results[i]<-ss[i]+1 } -thomas Thomas Lumley Assoc. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle
It is not very clear how you want to index your results vector. If ss contains the indices of the results vector that you are trying to change, this implies that you have a vector of length 9. In this case results <- numeric(max(ss)) results[ss] <- ss + 1 will do the trick. Or in case that ss contains the values that you want to augment by 1 results <- numeric(length(ss)) Results <- ss + 1 Am I missing something? -Christos -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Doran, Harold Sent: Monday, April 24, 2006 4:32 PM To: r-help at stat.math.ethz.ch Subject: [R] Store results of for loop I have what I'm sure will turn out to be straightforward. I want to store the results of a loop for some operations from a patterned vector. For example, the following doesn't give what I would hope for ss <- c(2,3,9) results <- numeric(length(ss)) for (i in seq(along=ss)){ results[i] <- i + 1 } The following does give what I expect, but creates a vector of length 9. ss <- c(2,3,9) results <- numeric(length(ss)) for (i in ss){ results[i] <- i + 1 } What I am hoping for is that results should be a vector of length 3. Harold [[alternative HTML version deleted]] ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
On Mon, 2006-04-24 at 16:31 -0400, Doran, Harold wrote:> I have what I'm sure will turn out to be straightforward. I want to > store the results of a loop for some operations from a patterned vector. > For example, the following doesn't give what I would hope for > > ss <- c(2,3,9) > results <- numeric(length(ss)) > for (i in seq(along=ss)){ > results[i] <- i + 1 > }Harold, Here you are getting:> results[1] 2 3 4 because 'i' is 1:3, thus:> 1:3 + 1[1] 2 3 4> The following does give what I expect, but creates a vector of length 9. > > ss <- c(2,3,9) > results <- numeric(length(ss)) > for (i in ss){ > results[i] <- i + 1 > }Here you are getting:> results[1] 0 3 4 NA NA NA NA NA 10 because 'i' is set to 'ss' which is c(2, 3, 9). Thus, 'results' is being indexed as results[c(2, 3, 9)]. You are adding 1 to 'ss' in the loop, thus:> ss + 1[1] 3 4 10 In short: results[ss] <- ss + 1 which yields:> results[1] 0 3 4 NA NA NA NA NA 10> What I am hoping for is that results should be a vector of length 3.I suspect what you want is: ss <- c(2, 3, 9) results <- numeric(length(ss)) for (i in seq(along = ss)) { results[i] <- ss[i] + 1 }> results[1] 3 4 10 You might also want to look at ?sapply, where you could do something like this:> sapply(ss, function(x) x + 1)[1] 3 4 10 HTH, Marc Schwartz