# [R] store variables in a for loop using get()

Duncan Murdoch murdoch.duncan at gmail.com
Mon Feb 11 13:39:46 CET 2013

```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 at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
{"ft_lang_label":"__label__en","ft_lang_prob":0.7581484,"math_prob":0.8938508,"size":2436,"snap":"2021-04-2021-17","text_gpt3_token_len":714,"char_repetition_ratio":0.11677632,"word_repetition_ratio":0.021978023,"special_character_ratio":0.3448276,"punctuation_ratio":0.15839694,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99646115,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-01-24T19:55:10Z\",\"WARC-Record-ID\":\"<urn:uuid:5487ffa9-0716-41be-8900-6419a5cfa9b5>\",\"Content-Length\":\"5867\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a00e8264-c427-4425-b584-556cc661504d>\",\"WARC-Concurrent-To\":\"<urn:uuid:ea552021-9406-447c-9d9e-6a7520d00bdd>\",\"WARC-IP-Address\":\"129.132.119.195\",\"WARC-Target-URI\":\"https://www.stat.math.ethz.ch/pipermail/r-help/2013-February/347258.html\",\"WARC-Payload-Digest\":\"sha1:CMH5I5YHV33XLAFMG35YBLN7GD6AVN2Q\",\"WARC-Block-Digest\":\"sha1:B5OYADX5OLOPVW73X5HPBTMKZDB56JCF\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-04/CC-MAIN-2021-04_segments_1610703550617.50_warc_CC-MAIN-20210124173052-20210124203052-00395.warc.gz\"}"}