With loop use columns in objects in R

2
Hello, I have created more than 8 thousand dataframes in R. However, when trying to loop in one of the columns of each object, I always have the answers that the function used does not read non-numeric objects. How do I fix this?

for(i in 10:13) { 
    nam <- paste("mu04mun_", i, sep = "")
    d <- paste0("sub04mun", i, "$des_flo_04")
    assign(nam, qnorm(d))
}
  

Error in qnorm (d): Non-numeric argument to math function

    
asked by anonymous 19.04.2018 / 16:37

1 answer

1

You are applying the qnorm() function to a text vector. See:

d <- "texto"
qnorm(d)
#> Error in qnorm(d): Non-numeric argument to mathematical function

Apparently your intention was to run qnorm() in the des_flo_04 column of the data frame sub04mun . For this, you need to call the object, not the text.

The suggestion of Rui will not work directly, because you can not put $ inside get , but you can do it as follows:

for(i in 10:13) { 
    nam <- paste("mu04mun_", i, sep = "")
    d <- paste0("sub04mun", i)
    assign(nam, qnorm(d)[["des_flo_04"]])
}

It is important to note that this type of code is not recommended. The ideal here is you work with lists (save the data frames in a list) and operate on top of the lists: #

21.04.2018 / 03:54