简体   繁体   中英

R: Cannot Open File : No such file or directory

I have script as below:

setwd ("I:/prep/Coord/RData/test")

#load .csv files
a.files <- grep("^Whirr", dir(), value=TRUE) #pattern matching
b.files <- paste0("Files_", a.files) 

for(i in length(a.files)){
    a <- read.table(a.files[i], header=T, sep=",", row.names=1) #read files start with     Whirr_
    b <- read.table(b.files[i], header=T, sep=",", row.names=1) #read files start with Files_
    a
    b

    cr <- as.matrix(a) %*% as.matrix(t(a)
    cr


    diag(cr)<-0
    cr

    #write to file
    write.csv(cr, paste0("CR_", a.files[i], ".csv"))
}

Basically, what I want to do is to compare two files which have similar filename at the end of file name, and do the calculation, and write the result to file.

When I tried to print a.files and b.files, the output seems ok for me. The output as below:

> a.files <- grep("^Whirr", dir(), value=TRUE) #pattern matching
> b.files <- paste0("Files_", a.files, sep="") 
Error: could not find function "paste0"
> a.files
[1] "Whirr_127.csv" "Whirr_128.csv"
> b.files
[1] "Files_ Whirr_127.csv" "Files_ Whirr_128.csv"
> 

I tried to feed the script with multiple files, but I got an error msg as below:

Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") : cannot open file 'Files_ Whirr_128.csv': No such file or directory

So, I tried to use file.choose, but it also doesn't work for me.

Appreciate help from the expert

Change the line:

b.files <- paste0("Files_", a.files) 

to:

b.files <- paste("Files_", a.files, sep="")

You are using a version of R that does not have paste0 (I see that code was given to you in an earlier answer). This means you were keeping an earlier version of b.files , perhaps one that had been constructed using paste .

One important lesson about this is that whenever you get an error message about a line, such as Error: could not find function "paste0" , that means the line did not happen! You have to fix that error before you paste the code, or tell us about the error when you do- otherwise we assume the b.files <- paste0("Files_", a.files) line works.

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