This isn't necessarily the best solution, but it should do the trick for you.
At the top of your outer loop, initialize a variable, tmp
here, with an empty list. Inside your inner loop, store the results into elements of that list using [[
. When your inner loop is finished, assign
the value in tmp
to a variable dynamically named using paste0
.
dat <- vector(mode = "list")
i <- numeric(13)
n <- numeric(4)
for(i in 6:18){
tmp = list()
for(n in 1:4){
path <- paste0("C:/.../downloads/EIS_Chip",i,"_", n, "_pre.dat")
list[[n]] <- read.csv(file = path)
}
assign(paste0("dat_",i),tmp)
}
A better way to approach this though could be to use expand.grid
to get every combination of i
and n
, then write a function that reads in a file using those values. Finally, using something like purrr::pmap
to iterate over your values. This will return a list with 52 elements, one for each file you loaded.
read_in_file = function(i,n){
path = paste0("C:/.../downloads/EIS_Chip",i,"_", n, "_pre.dat")
return(read.csv(file=path))
}
combinations = expand.grid(i = 6:18, n = 1:4)
dat = purrr::pmap(combinations,read_in_file)
This works because expand.grid
returns a data.frame
which is a special kind of list. Because we named the inputs appropriately, purrr::pmap
processes the list of inputs and assigns them correctly to the function.