0

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

Nafis S.
  • 303
  • 1
  • 2
  • 10

2 Answers2

1

Congratulations for transitioning to list's from the messy get's and assign's

I have not tested this but this could simplify processing

PLM_List = ls(pattern="^plm_f_*");


fn_ProcessDF = function(PLMObj = PLMname,replacementDF = female_q, colIndices = 1:26) {

PLMObj = get(PLMObj)

PLMObj$coefficients[colIndices] = replacementDF[,colIndices]

PLMObj = PLMObj

}

lapply(1:length(PLM_List),function(x) fn_ProcessDF(PLMObj = x ,replacementDF = female_q[x,], colIndices = 1:26))
Silence Dogood
  • 3,587
  • 1
  • 13
  • 17
0

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]
Nafis S.
  • 303
  • 1
  • 2
  • 10