[R] Store results of for loop

Christos Hatzis christos at silicoinsights.com
Mon Apr 24 22:49:31 CEST 2006

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?


-----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.


	[[alternative HTML version deleted]]

R-help at stat.math.ethz.ch mailing list
PLEASE do read the posting guide!

More information about the R-help mailing list