简体   繁体   English

R:无法读取粘贴文件名的csv数据

[英]R: can't read csv data of pasted file name

I'm writing a universal script to read out the data for an online tool. 我正在编写一个通用脚本来读取在线工具的数据。 This is my code so far: 到目前为止,这是我的代码:

d <- list.files("E:/...path.../demo_subjects")
d <- rep(d, each=4)
f <- c("0.csv", "1.csv", "2.csv", "3.csv")
l <- paste0(d,"/", f)
n <- paste0("E:/...path.../demo_subjects/",l)

for (i in 1:length(d)) {
  x[i] <- read.csv(n, sep=",", header = TRUE)
}

Returns me this error: 返回此错误:

Error in file(file, "rt") : invalid 'description' argument file(file,“ rt”)中的错误:无效的'description'参数

After some intensive googling, I haven't found an answer to this yet. 经过密集的谷歌搜索,我还没有找到答案。 For some people, the issues had to do with invalid file paths, but 对于某些人来说,问题与无效的文件路径有关,但是

file.exists(n)

returns all TRUE. 返回所有TRUE。

Try this: 尝试这个:

path <- 'yourpath'
d <- list.files(path)
d <- rep(d, each=4)
f <- c("0.csv", "1.csv", "2.csv", "3.csv")
l <- paste0(d,"/", f)
n <- paste0(path,l)
x <- list()

for (i in 1:length(d)) {
  x[[i]] <- read.csv(n[i], sep=",", header = TRUE)
}

As far as I know, argument file in read.csv is not vectorized so probably you forgot to index n: ...... read.csv(n[i], sep = "," ......... 据我所知,read.csv中的参数文件未向量化,因此您可能忘记了索引n:...... read.csv(n [i],sep =“,” ....... ..

Besides, you have to asign this output to an element of a list! 此外,您必须将此输出分配给列表的元素! So use double claudators for indexing x and initialize an empty list (x <- list()) before the for loop. 因此,请使用双束线符为x编制索引,并在for循环之前初始化一个空列表(x <-list())。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM