【问题标题】:run script multiple times on .txt files and save each output in single table in r在 .txt 文件上多次运行脚本并将每个输出保存在 r 中的单个表中
【发布时间】:2016-03-26 16:31:39
【问题描述】:

我必须对 100 多个 .txt 文件进行相关性分析。我有一个脚本,它读取单个文件,以我需要的适当方式组织数据,然后将相关值存储为新变量。该脚本非常大,因为数据被重新格式化了很多。

我的问题。如何使此脚本在所有 100+ .txt 文件上重复运行,并将所有 100+ 的单个相关值存储在单个 DF 中?理想情况下,最终的 DF 将由两列组成,一列带有 .txt ID,另一列带有相关系数,它将有 100 多行。

我可以直接将脚本复制并粘贴到 for 循环中吗?如果是这样,那将如何出现?我是新手! 有任何想法吗? 谢谢!

【问题讨论】:

  • 使用lapply 在多个文件上运行。

标签: r loops repeat


【解决方案1】:

正如akrun 提到的,您可以使用lapply 执行此操作。在没有看到你的数据的情况下,我会推荐这样的东西:

my.files <- list.files(pattern = "txt")  # use a pattern that only matches the files you want to read in
output <- lapply(my.files, correlation_function)

# Combine list of outputs into a single data.frame
output.df <- do.call(rbind, output)

这假设您有一个名为 correlation_function 的函数,该函数将文件名作为输入,loads 将文件转换为 R,运行相关性分析,并返回 data.frame

【讨论】:

  • 谢谢!我试试看!
猜你喜欢
  • 2019-02-04
  • 1970-01-01
  • 2022-12-19
  • 1970-01-01
  • 2014-11-21
  • 2015-05-01
  • 1970-01-01
  • 2022-01-04
  • 1970-01-01
相关资源
最近更新 更多