简体   繁体   中英

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

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.

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 = "," .........

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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