[R] looping over a string

Thomas Schwander r_lover at web.de
Thu Jan 22 20:37:55 CET 2009


Hi list,

I'm using R 2.8.1 under Windows vista. I have the following problem:

First of all I create a string-vector. Then I "convert" these strings 
into variables and assign a vector of numeric values. So far 
everything's fine.
Now I want to do nearly the same again: I create another string-vector 
and I want to assign the variance. So I have to loop over the first 
string-vector.
But this does not work unfortunately. I looked in the R-list-search but 
I cannot find the right answer.

Please find enclosed the pseudo-code:

#Tage zwischen berechnung und 31.03.2009
Berechnung<-as.Date("22.01.2009","%d.%m.%Y")
Enddatum<-as.Date("31.03.2009","%d.%m.%Y")
Tage<-difftime(Enddatum, Berechnung)
Tage<-as.numeric(substr(format(Tage),1,2))

#maximal interessierend sind 100 Tage (4 Monate)-Differenzen
max_int<-82
Tage<-max_int
max_bob<-82

varnames<-paste("st_rendite_",seq(max_int),sep="")
analyse<-rnorm(10000)
for(i in 1:min(max_bob,max_int)){
  assign(varnames[i],diff(log(analyse),i)*100)
}

#Wurzel_t - Approximation überprüfen
varianzen<-paste("var_",seq(Tage),sep="")
for(i in 1:Tage){
#in this line is the error I cannot handle (st_rendite_i):
  assign(varianzen[i],var(st_rendite_i)/100^2)
}

Thanks for your help,
Thomas




More information about the R-help mailing list