On 13-02-11 6:58 AM, ivan wrote:
Dear R list,

I have a problem in assigning values to existing data frames in R. I have a
vector x containing the names of the data frames, which I create to store
the results for each variable (a1,a2,a3) obtained in time series moving
regressions. Thus, say,

x=c('a1','a2','a3')

Moreover, b is a vector containig unique dates of the points in time of the
moving regressions. Thus, say,

b=c('2012-11-30','2012-12-31')

I create the data matrices in the following way:

   for (i in 1:(length(x))){
        assign(
                paste(x[i]),
                        matrix(NA,ncol=5,nrow=length(b),
                                dimnames=list(paste(b), 
c('Estimate','Std.Error','t value','p
value','R-squared'))))
   }    


After doing so, I want to store the results of the regressions in the
following way:

   for (i in 1:length(x)){
        for (j in 1:length(b)){
                get(x[i])[j,1]<-summary(get(paste(b[j])))$coefficients[i,1]
                get(x[i])[j,2]<-summary(get(paste(b[j])))$coefficients[i,2]
                get(x[i])[j,3]<-summary(get(paste(b[j])))$coefficients[i,3]
                get(x[i])[j,4]<-summary(get(paste(b[j])))$coefficients[i,4]
                get(x[i])[j,5]<-summary(get(paste(b[j])))$r.squared
     }
   }

However, R produces an error. The reason is that it seems that using
the get() function on the left hand side in the above expressions is
inappropriate in the for loop.

Does anybody have an idea?

Don't do that. There are ways to do what you are trying to do, but you should just handle this differently. The most obvious way is to have a 3 element list of dataframes instead of three separate dataframes.

So instead of get(x[i][j,1] <- ... you would simply use a[[i]][j, 1].

Other comments: paste(x[i]) is identical to x[i] when x[i] is a character value. Don't bother with the paste.

Do you really have variables named '2012-11-30', etc.? They can't be very easy to work with. Don't do that either.

Duncan Murdoch


Thank you very much in advance.

Regards

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to