2016-10-19 3 views
0

수정 경로와 1 년 중 매우 제한된 디렉토리가 주어집니다. 나는 각이 초기 조합 사이의 경로의 조합 (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" 
이 문제를 해결하기 위해 내 접근 방식을 사용입니다

A의 루프 :

  1. setwd(pathVector[1])
  2. 와 작업 디렉토리가 채워진 가방을 스캔 설정 루프의 각 반복에서 벡터에 paste(pathVector[1], list.files(pathVector[1]), sep = "/")
  3. 저장이 조합을하고 다음 반복

진행 ...하지만 : 그 작업 디렉토리에 list.files와 ES (하위 디렉토리) 및 각 조합에 당첨되는 것 조합 수는 많아서 각 반복마다 하나 이상의 저장 방법을 알아낼 수 없습니다..

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]) 
} 

그래서, 방법 for 루프의 각 반복에서 여러 값 (조합)을 저장할 수 있습니다 : 여기 내 코드는?

나는 예를 들어, 모든 조합을 포함하는 벡터를 원하는 :

"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" 
+2

'list.files (full.names = TRUE, recursive = TRUE)'이 작업에 도움이 되나요? – roman

+0

아니요, 작동하지 않습니다. 'full.names = TRUE'와'recursive = TRUE'를 어떻게 적용 할 수 있습니까? 'list.files (fixPath, full.names = TRUE, recursive = TRUE)와 작동하지 않기 때문에' –

+0

list.files (fixPath, recursive = TRUE, include.dirs = TRUE)'완벽하게 작동합니다! –

답변

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]) 
} 
+1

할당 연산자 "c"는 모든 조합의 캡슐화 작업을 수행하지만 인쇄 기능은 첫 번째 반복의 조합 만 표시합니다. 그런데 당신의 대답에 감사드립니다! –

+0

그래, 우리는 중간에있다! :) 모든 반복마다 모든 하위 디렉토리를 인쇄하고 싶습니까? 붙여 넣기 인수를 인쇄 할 수 있기 때문에 ('paste (pathVector [i], list.files (pathVector [i]), sep = "/")'). 또는 각 반복마다 모든 하위 디렉토리가있는 목록을 갖고 싶습니까? – User2321

1

당신은 사전에 벡터를 설정 한 다음에이 부분을 사용하려고 할 수 귀하의 루프 :

append(VectorName, pathFinal[i]) 

이와 같이 기존 코드에 포함하려고 시도 할 수도 있습니다.

pathFinal <- append(pathFinal, paste(pathVector[i], list.files(pathVector[i]), sep = "/")) 

I hav 체크하지 마라. 그러나 원하는 각각의 새로운 값을 벡터에 추가해야한다. 또한 setwd()을 사용할 필요가 없다고 생각합니다.

+0

또는'foreach' 라이브러리를 사용하십시오. – Henk

+0

append 함수는 각 반복에서 모든 조합을 캡슐화하는 작업을 수행합니다. 당신 말이 맞아요. "setwd"의 사용은 불필요합니다. 고마워! –

관련 문제