【问题标题】:Use a for-loop to create list-objects for each file from file directory使用 for 循环为文件目录中的每个文件创建列表对象
【发布时间】:2019-08-21 15:07:59
【问题描述】:

我有一个创建的文件列表:

d <- list.files(path = "C:/data/", pattern = ".TextGrid") 

我想在每个文件上运行 textgRid 包的 TextGrid() 命令。这将为每个文件创建一个列表对象。这些我想以文件名作为对象名保存为列表对象。

我试过了:

for (file in d) {file <- TextGrid(paste0("C:/data", file))}

感谢您的帮助

【问题讨论】:

  • 哪种语言,也许用那个标记
  • true...in R. Thx!

标签: r list for-loop


【解决方案1】:

以您的代码为起点:

for (file in d) {
    tmp <- TextGrid(paste0("C:/data/", file))
    assign(file, tmp)
}

【讨论】:

  • 完美,只是缺少后挡板for (file in d) { tmp &lt;- TextGrid(paste0("C:/data/", file)) assign(file, tmp) }
  • 哪个反斜杠?
  • 文件路径中的反斜杠
【解决方案2】:

这是我使用 lapply 而不是循环的解决方案,我从未使用过 textgRid 包,所以希望它可以工作。

d <- lapply(list.files(pattern = "\\.TextGrid$"),TextGrid)
names(d)<-paste0("C:/data",list.files(pattern = "\\.TextGrid$")

【讨论】:

    猜你喜欢
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多