subset data in a loop for R

2

I'm doing a script in R where a for loop iterates over a vector string called iter. The following code works and generates a variable named as the value that iter takes at that moment (i) that contains the filtering of a dataframe according to i.

for ( i in iter) {
  assign(i, subset(out,TestId==paste0(i) & AddId=="Curva_F_Cor"))
}

The problem comes when I want to get a specific column of the variable I created inside the for loop with the following code:

for ( i in iter) {
  assign(i, subset(out,TestId==paste0(i) & AddId=="Curva_F_Cor"))
  assign(paste0(i,".mu"), i[,"mu.spline"])
}

Result in this error:

Error in i[, "mu.spline"] : incorrect number of dimensions

If I try this, the code if it works, I have the problem when I get the mu.spline column in this way, although done manually if it works

for ( i in iter) {
  assign(i, subset(out,TestId==paste0(i) & AddId=="Curva_F_Cor"))
  i
  assign(paste0(i,".mu"), "hola")
}

What can be wrong?

Thanks

Edited ** OS I leave a dput () of my data:

structure(list(TestId = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 
3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 
8L, 9L, 9L, 9L, 10L, 10L, 10L, 11L, 11L, 11L, 12L, 12L, 12L, 
13L, 13L, 13L, 14L, 14L, 14L, 15L, 15L, 15L, 16L, 16L, 16L, 17L, 
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L), .Label = c("Comb1", 
"Comb2", "COD1", "COD2", "COD3", "COD4", "COD5", 
"COD6", "COD7", "COD8", "COD9", "COD10", "COD11", 
"COD12", "COD13", "COD14", "Pat"), class = "factor"), 
    mu.spline = c(0.156373645710651, 0.128179004733465, 0.133922208832118, 
    0.0968325365246728, 0.112497378553166, 0.108787192266453, 
    0.110192954818258, 0.121005105680758, 0.0980394197157738, 
    0.138420857616108, 0.127789639429687, 0.128560390185466, 
    0.110549423439033, 0.108320566548023, 0.098918312107995, 
    0.0828284492044932, 0.104197889210497, 0.122413067260436, 
    0.100261893863431, 0.0938211089313908, 0.0950013179641027, 
    0.145680825059066, 0.139104408376977, 0.126037019624304, 
    0.126708418382696, 0.129821223842992, 0.136480998324424, 
    0.13593684872676, 0.139066913195263, 0.148222162331793, 0.1063086971118, 
    0.167178433353777, 0.0999504815546864, 0.159110219357191, 
    0.125081233896366, 0.163966026506179, 0.15029944955429, 0.116975580695436, 
    0.15276496804095, 0.155339014181045, 0.112171217970295, 0.120104234834245, 
    0.133373734309075, 0.175784287024805, 0.133626401899954, 
    0.140297143337283, 0.0863206151811713, 0.170070971923806, 
    0.152896880973888, 0.10553437562759, 0.124122727198564, 0.163571762302165, 
    0.151047108367937, 0.131416085292366, 0.152515440225195, 
    0.139308623745812, 0.146009754853497, 0.170825235429307, 
    0.147466868348918, 0.126623691613807, 0.147114348605148, 
    0.141084369853073, 0.153670399861141, 0.162948873362462, 
    0.131121302899353, 0.146421599771427, 0.135166111999851, 
    0.157495164357944, 0.126927329131488, 0.159831796004744, 
    0.146936913846553, 0.12183336770971, 0.136669798817364, 0.152333836640196, 
    0.138055091325892)), .Names = c("TestId", "mu.spline"), row.names = c("76", 
"77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", 
"88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", 
"99", "100", "101", "102", "103", "104", "105", "106", "107", 
"108", "109", "110", "111", "112", "113", "114", "115", "116", 
"117", "118", "119", "120", "121", "122", "123", "124", "125", 
"126", "127", "128", "129", "130", "131", "132", "133", "134", 
"135", "136", "137", "138", "139", "140", "141", "142", "143", 
"144", "145", "146", "147", "148", "149", "150"), class = "data.frame")
    
asked by Neuls 10.02.2017 в 09:55
source

1 answer

0

In the second line you call i[,"mu.spline"]) , since the object i does not exist, you used it to create objects with names based on vector iter based on the position i . A practical solution is to use a temporary object inside the loop to be able to extract the necessary variables without problem. For example:

for(i in iter){
  temp <- subset(out,TestId==paste0(i))
  assign(i, temp)
  assign(paste0(i,".mu"), temp[,"mu.spline"])
}

With this slight modification of the code you will not have problems, then you can delete it if necessary to save memory in the workspace.

Consider using this scheme within the loops to avoid errors with the names of objects created within the same loop. You can also create an empty list prior to the execution of the loop and store the results in the list. But in this case, the option to use a temporary element is the most reasonable and lightweight.

    
answered by 12.02.2017 / 02:50
source