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