2017-05-13 5 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

こんにちはマット、あなたの答えをありがとう。ループは、私がやろうとしていたことに対してうまくいきました。将来の読者のための注意点として、変数 'target_id'の' filter_173'ベクタに対応する行をフィルタリングしたいので、最後に '、'を追加することが重要でした。 'write.table'のデフォルトでは、' quotes = T'と 'row.names = T'を持っています。これは、タブ区切りファイル(私が推測している)に敏感な場合、将来のスクリプトを破るでしょう。 "したがって、効果的に以下 –

+0

が働いたループである: 'のための(I 1:長さ(predict_full)) {#はデータフレーム に現在のファイルをロードdf.predict < - read.tableを(predict_full [i]は、ヘッダ= T、9月= "\ tの")不要な行 df.predict <アウト #フィルタ - df.predict [!%のfilter_173中(df.predict $ TARGET_ID%)、] #は、フィルタリングデータフレームを書きます新しいディレクトリに追加します write.table(df.predict、file = file.path(filteredpath、paste(sample_names [i]、 "filt_predict.txt")、sep = "\ t"、row.names = F、quotes = F)) } ' –

関連する問題