固定パスと、年間のディレクトリが非常に限られているとします。 、および異なる、非固定と非均等量fixPathの各組み合わせに含まれるサブディレクトリを - 私はこの最初の組み合わせ(年fixPath) - との間の経路の各組み合わせを取得しようとしている年forループの各繰り返しから複数の出力を格納します。
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"
この問題を解決するための私のアプローチは、forループを使用です:
- は
setwd(pathVector[1])
- での作業ディレクトリを設定しFILをスキャンその作業ディレクトリ内
list.files
とES(サブディレクトリ)で、それぞれの組み合わせを得る:paste(pathVector[1], list.files(pathVector[1]), sep = "/")
- ストアベクトルで、この組み合わせとループの各反復から次の反復
...しかしを進めます私にはたくさんの組み合わせがあり、繰り返しのたびに複数の格納方法を理解することはできません。ここに私のコードは次のとおりです。
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])
}
だから、はどのようにするループの各反復から複数の値(の組み合わせ)を保存できますか?
私は、たとえば、すべての組み合わせが含まれているベクトルをしたい:
"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"
このタスクで 'list.files(full.names = TRUE、recursive = TRUE)'は役に立ちますか? – roman
いいえ、動作しません。 'full.names = TRUE'と' recursive = TRUE'をどのように適用できるのか? (fixPath、full.names = TRUE、recursive = TRUE) ' –
' list.files(fixPath、recursive = TRUE、include.dirs = TRUE) 'うまく動作します! –