gpt4 book ai didi

根据 R 中的选择标准从目录中读取选定的文件

转载 作者:行者123 更新时间:2023-12-04 10:28:39 25 4
gpt4 key购买 nike

我想只读取文件夹中选定的 .txt 文件来构建一个巨大的表格...我有超过 9K 的文件,并且想导入具有选定距离和建筑类型的文件,这在部分中有说明文件名。

例如,我想先选择名称中包含“_U0”和“_0_Final.txt”的文件:

Type = c(0,1)
D3Test = 1
Distance = c(0,50,150,300,650,800)
D2Test = 1;

files <- list.files(path=data.folder, pattern=paste("*U", Type[D3Test],"*_",Distance[D2Test],"_Final.txt",sep=""))

但是返回的结果是空的...我的构造有问题吗?

 filename <- scan(what="")
"M10_F1_T1_D1_U0_H1_0_Final.txt" "M10_F1_T1_D1_U0_H1_150_Final.txt" "M10_F1_T1_D1_U0_H1_300_Final.txt"
"M10_F1_T1_D1_U0_H1_50_Final.txt" "M10_F1_T1_D1_U0_H1_650_Final.txt" "M10_F1_T1_D1_U0_H1_800_Final.txt"
"M10_F1_T1_D1_U0_H2_0_Final.txt" "M10_F1_T1_D1_U0_H2_150_Final.txt" "M10_F1_T1_D1_U0_H2_300_Final.txt"
"M10_F1_T1_D1_U0_H2_50_Final.txt" "M10_F1_T1_D1_U0_H2_650_Final.txt" "M10_F1_T1_D1_U0_H2_800_Final.txt"
"M10_F1_T1_D1_U0_H3_0_Final.txt" "M10_F1_T1_D1_U0_H3_150_Final.txt" "M10_F1_T1_D1_U0_H3_300_Final.txt"
"M10_F1_T1_D1_U0_H3_50_Final.txt" "M10_F1_T1_D1_U0_H3_650_Final.txt" "M10_F1_T1_D1_U0_H3_800_Final.txt"
"M10_F1_T1_D1_U1_H1_0_Final.txt" "M10_F1_T1_D1_U1_H1_150_Final.txt" "M10_F1_T1_D1_U1_H1_300_Final.txt"
"M10_F1_T1_D1_U1_H1_50_Final.txt" "M10_F1_T1_D1_U1_H1_650_Final.txt" "M10_F1_T1_D1_U1_H1_800_Final.txt"

最佳答案

另一种方法是使用 sprintfgrepl

x <- c("M10_F1_T1_D1_U0_H1_150_Final.txt", "M10_F1_T1_D1_U0_H2_650_Final.txt", "M10_F1_T1_D1_U1_H1_650_Final.txt")

x[grepl(sprintf("U%i_H%i_%i", 1, 1, 650), x)]

[1] "M10_F1_T1_D1_U1_H1_650_Final.txt"

关于根据 R 中的选择标准从目录中读取选定的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31188628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com