2017-05-13 10 views
0

코드의 일부를 알아 냈으므로 아래에서 설명 하겠지만 함수를 반복 (루프)하는 것은 어렵습니다. 파일 목록 :디렉토리의 여러 파일에 루프 하위 집합을 지정하고 접미어가 붙은 새 디렉토리에 파일을 출력합니다.

library(Hmisc) 
filter_173 <- c("kp|917416", "kp|835898", "kp|829747", "kp|767311") 
# This is a vector of values that I want to exclude from the files 
setwd("full_path_of_directory_with_desired_files") 
filepath <- "//full_path_of_directory_with_desired_files" 
list.files(filepath) 
predict_files <- list.files(filepath, pattern="predict.txt") 
# all files that I want to filter have _predict.txt in them 
predict_full <- file.path(filepath, predict_files) 
# generates full pathnames of all desired files I want to filter 
sample_names <- sample_names <- sapply(strsplit(predict_files , "_"), `[`, 1) 

다음은 간단한 예제 파일로 특정 예제 파일과 함께하고 싶습니다. 접미사 원래 필터링으로 어떻게 내가 같은 이름의 폴더에 필터링 된 파일을 배치 어떻게 마지막으로 predict_full

test_predict <- read.table("a550673-4308980_A05_RepliG_rep2_predict.txt", header = T, sep = "\t") 
# this is a file in my current working directory that I set with setwd above 
test_predict_filt <- test_predict[test_predict$target_id %nin% filter_173] 
    write.table(test_predict_filt, file = "test_predict") 

에있는 모든 파일 이름에 루프에서이 작업을 반복합니까?

predict_filt <- file.path(filepath, "filtered") 
# Place filtered files in 
filtered/ subdirectory 
filtPreds <- file.path(predict_filt, paste0(sample_names, "_filt_predict.txt")) 

항상 루핑에 고생했습니다. 모든 사람의 작업 디렉토리와 파일 경로가 고유하므로 100 % 재생산 가능한 예제를 공유하는 것은 어렵습니다. 공유 한 모든 코드가 시스템의 적절한 경로 이름에 적용되면 작동합니다.

답변

0

이렇게하면 각 파일을 반복하고 필요한 파일 이름 사양을 사용하여 새 위치에 쓸 수 있습니다. 먼저 디렉토리 경로를 변경하십시오.

filter_173 <- c("kp|917416", "kp|835898", "kp|829747", "kp|767311") #This is a vector of values that I want to exclude from the files 

filepath <- "//full_path_of_directory_with_desired_files" 
filteredpath <- "//full_path_of_directory_with_filtered_results/" 

# Get vector of predict.txt files 
predict_files <- list.files(filepath, pattern="predict.txt") 

# Get vector of full paths for predict.txt files 
predict_full <- file.path(filepath, predict_files) 

# Get vector of sample names 
sample_names <- sample_names <- sapply(strsplit(predict_files , "_"), `[`, 1) 

# Set for loop to go from 1 to the number of predict.txt files 
for(i in 1:length(predict_full)) 
{ 
    # Load the current file into a dataframe 
    df.predict <- read.table(predict_full[i], header=T, sep="\t") 

    # Filter out the unwanted rows 
    df.predict <- df.predict[!(df.predict$target_id %in% filter_173)] 

    # Write the filtered dataframe to the new directory 
    write.table(df.predict, file = file.path(filteredpath, paste(sample_names[i],"_filt_predict.txt",sep = ""))) 
} 
+0

안녕하세요, Matt, 답장을 보내 주셔서 감사합니다. 루프는 의도 한대로 작동했습니다. 미래 독자를위한 몇 가지주의 사항, 변수'target_id'에서'filter_173' 벡터에 해당하는 행을 필터링하고 싶었 기 때문에 마지막에', '를 추가하는 것이 중요했습니다. 또한'write.table'에서'sep'는 파일 함수가')'로 닫힌 후에야합니다.'write.table'의 기본값은 어떻게 든'quotes = T'와'row.names = T'를가집니다. 탭으로 구분 된 파일 (""될 것 같음)에 민감한 경우 미래 ​​스크립트를 중단시킬 수 있습니다. 따라서 효과적으로 다음 –

+0

근무 루프이다 '대 (I (1) : 길이 (predict_full)) { #가 dataframe 로 현재 파일을로드 df.predict <- read.table (predict_full [I] 헤더 T = 9 월 = \ "를 t")은 원하지 않는 행 아웃 # 필터 df.predict <- [! %의 filter_173에서 (df.predict $ target_id %)] df.predict # 작성 필터링 dataframe 새 디렉토리에 write.table (df.predict, file = file.path (filteredpath, paste (sample_names [i], "_ filt_predict.txt"), sep = "\ t", row.names = F, quotes = F)) }' –