Search code examples
rloopsvectorplm

Trying to loop through "plm" objects in R and replace coefficients (atomic vector, non-language)


Note: I've super new to R (just transitioning from Stata)!

Hi guys! I have a large number of "plm" objects numbered as plm_f_1_1, plm_f_1_2,..., plm_f_1_17, plm_f_2_1,... and so on, and I have data frames of coefficients stored, let's say in "female_q[1,]". I'm trying to loop through the "plm" objects and try to do this operation:

for (i in 1:26) {
    plm_f_1_1$coefficients[i]=female_q[1,i]
}

So this works for one plm object, when I try to loop over the last number in the object name, I get this error (just showing for one point in the female_q data frame) when I try get:

get(paste0("plm_f_1_",i))$coefficients[1]=female_q[1,1]

Error in get(paste0("plm_f_1_", i))$coefficients[1] = female_q[1,1] : 
  target of assignment expands to non-language object

and this one with assign:

assign(paste0("plm_f_1_",i)$coefficients[1],1)

Error in paste0("plm_f_1_", i)$coefficients : 
  $ operator is invalid for atomic vectors

Here are some descriptions on how the structures look like:

str(get(paste0("plm_f_1_",i))$coefficients)
 Named num [1:26] 0.1362 -0.1835 -0.3464 0.2858 -0.0634 ...
 - attr(*, "names")= chr [1:26] "(Intercept)" "dem_log_gdppc_5" "dem_log_mat_educ_5" "dem_log_pop_15_share_5" ...


get(paste0("plm_f_1_",i))$coefficients[1]
(Intercept) 
  0.1361659 

female_q[1,1]
[1] 0.1314744

I'm wondering if I have to change the way female_q is being called, or something along those lines? Would appreciate any help! Thanks!!!


Solution

  • Never mind, I got it: I put all my plm objects in a list as such, and then I didn't have to use the get or assign command, and it works perfectly:

    plm_female_all[[i]]$coefficients[x] = female_q[1,x]