Given a fix path and a very limited directories from year. I'm trying to obtain each combination of path between this initial combination (fixPath - year) and the different, non-fixed and non-equally quantity, subdirectories contained in each combination of fixPath - year
fixPath <- "C:/Users/calcazar/Desktop/example"
year <- 2008:2010
pathVector <- paste(fixPath, year, sep = "/")
pathVector
[1] "C:/Users/calcazar/Desktop/example/2008" "C:/Users/calcazar/Desktop/example/2009"
[3] "C:/Users/calcazar/Desktop/example/2010"
My approach to solve this problem is use a for-loop:
setwd(pathVector[1])
list.files
in that working directory and obtain each combination with: paste(pathVector[1], list.files(pathVector[1]), sep = "/")
...but from each iteration of the loop I have a bunch of combinations and I can't figure out how to store more than one for each iteration. Here is my code:
for (i in seq_along(pathVector)) {
setwd(pathVector[i])
# here I only obtain the combination of the last iteration
# and if I use pathFinal[i] I only obtain the first combination of each iteration
pathFinal <- paste(pathVector[i], list.files(pathVector[i]), sep = "/")
# print give me all the combinations
print(pathFinal[i])
}
So, how can store multiple values (combinations) from each iteration in a for loop?
I want a vector that contain all the combinations, for example:
"C:/Users/calcazar/Desktop/example/2008/a"
"C:/Users/calcazar/Desktop/example/2008/z"
"C:/Users/calcazar/Desktop/example/2009/b"
"C:/Users/calcazar/Desktop/example/2009/z"
"C:/Users/calcazar/Desktop/example/2009/y"
"C:/Users/calcazar/Desktop/example/2010/u"
答案 0 :(得分:1)
您可以尝试事先设置矢量,然后在for循环中使用此部分:
append(VectorName, pathFinal[i])
您可以尝试将其包含在现有代码中,例如
pathFinal <- append(pathFinal, paste(pathVector[i], list.files(pathVector[i]), sep = "/"))
我没有检查过,但它应该将每个新值添加到您想要的向量中。另外,我认为您不需要使用setwd()
。
答案 1 :(得分:1)
这样的事情能做你想做的吗?
pathFinal = NULL
for (i in seq_along(pathVector)) {
setwd(pathVector[i])
pathFinal <- c(pathFinal, paste(pathVector[i], list.files(pathVector[i]), sep = "/"))
print(pathFinal[i])
}