R 在目录中的多个文件上循环子集,并将文件输出到带有后缀的新目录中
我已经理解了代码的某些部分,我将在下面进行描述,但我发现很难在文件列表上迭代(循环)函数:R 在目录中的多个文件上循环子集,并将文件输出到带有后缀的新目录中,r,apply,mapply,R,Apply,Mapply,我已经理解了代码的某些部分,我将在下面进行描述,但我发现很难在文件列表上迭代(循环)函数: 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")
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)
最后,如何将过滤后的文件放在与原始文件同名且后缀为filtered的文件夹中
predict_filt <- file.path(filepath, "filtered")
# Place filtered files in
filtered/ subdirectory
filtPreds <- file.path(predict_filt, paste0(sample_names, "_filt_predict.txt"))
predict\u filt这应该可以循环遍历每个文件,并使用所需的文件名规范将它们写入新位置。只需确保先更改目录路径即可
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 = "")))
}
filter_173嗨,马特,谢谢你的回答。这个循环对我打算做的事情很有效。对于未来的读者,我只是想提醒一下,我想过滤变量target\u id
中与filter\u 173
向量对应的行,因此在末尾添加一个,
非常重要。同样在write.table
中,sep
应该在文件函数用关闭后出现write.table
中的默认值有quotes=T
和row.names=T
。如果将来的脚本对制表符分隔的文件中的“”敏感(我猜它们会是这样),那么这将破坏您的脚本。因此,有效地执行以下循环:for(I in 1:length(predict#full)){#将当前文件加载到dataframe df.predict中
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 = "")))
}