0

I am trying to repeat the same process multiple times across multiple data sources and rather than write out each process I want to use a for loop but I am having trouble changing the names of the data source and output object.

In an example I have three data sources called sim1_1, sim1_2 and sim1_3 and I want to do a calculation on each one to produce output supp1_1, supp1_2 and supp1_3. So I have two vectors of the names of the data sources and the output I want to create.

data <- c("sim1_1", "sim1_2", "sim1_3")             
supp <- c("supp1_1", "supp1_2", "supp1_3") 

so rather than write:

supp1_1 <- sim1_1$epi$tx.full.supp/sim1_1$epi$diag
supp1_2 <- sim1_2$epi$tx.full.supp/sim1_2$epi$diag

etc..

I want to use a loop where supp[i] and data[i] are defined by the location i on the vectors I defined above. I can define the text I want from the vectors but the text isn't applied in the object name. See the example below.

data[i]
# [1] "sim1_1"
head(sim1_1$epi$tx.full.supp[1])
#   sim1
# 1    0
# 2    0
# 3    0
# 4    0
# 5    0
# 6    0

head(data[i]$epi$tx.full.supp[1])
# Error in data[i]$epi : $ operator is invalid for atomic vectors
Hack-R
  • 22,422
  • 14
  • 75
  • 131
Deven
  • 1
  • 1
    Can you please make a minimally reproducible example? http://stackoverflow.com/help/mcve – dayne Jul 22 '16 at 18:32
  • Maybe take a look at http://stackoverflow.com/documentation/r/2201/for-loops#t=201607221834422385096 – dayne Jul 22 '16 at 18:34
  • 1
    This has been answered many times before. You can just `paste` a name into `assign`. – Hack-R Jul 22 '16 at 19:03
  • 1
    Possible duplicate of [Change variable name in for loop using R](https://stackoverflow.com/questions/16566799/change-variable-name-in-for-loop-using-r) – Andrew Haynes Dec 18 '17 at 10:08

0 Answers0