【发布时间】:2019-10-07 15:42:44
【问题描述】:
目前我一直在使用 R 读取表格并绘制一些我保存为 png 文件的数据。现在我有 100 个文件,希望这个过程能够自动化,而不是手动更改路径 100 次。
此外,我想将 100 个文件加入 R 中的一个表中,以便随后进行分析。连接将采用 dplyr 的 bind_rows 格式,因为所有文件都具有相同的列标题。当我在 R 中有两个表时,我已经这样做了,但现在当我使用循环按顺序读取文件时。在 R 中执行此操作的最佳方法是什么?提前感谢您的任何建议或帮助。
my_data <- read.table('/path/to/data/results/sample_1.txt', header = TRUE, sep = "\t")
ggplot(my data, aes(x=alt_freq)) + geom_histogram(color="black", fill="white", bins = 20) + xlim(c(0,1))
ggsave("/path/to/plots/sample_1.png", plot = last_plot(),width = 16, height = 9)
#append table to one large table in the format of dplyr::bind_rows(y, z)
输入文件都以相同的命名约定命名:
sample_1.txt
sample_2.txt
sample_3.txt
文件看起来像:
sample_name position alt_freq ref_freq sample_1_counts
sample 1 10 0.5 0.5 2
sample 1 20 0.25 0.75 4
所有的txt文件都在同一个目录下,所有的txt文件都是感兴趣的。
【问题讨论】:
-
所有文件都在同一个目录下吗?是否所有感兴趣的目录中的
.txt文件? -
是的,没错
标签: r loops ggplot2 plot dplyr