Rookie error

Hi everyone =)

I'm relatively new to R and I'm trying to make this work code, but it doesn't. It still gives me the error

"Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file '14116.1 CX1 CP1-1.nd2 (Log).txt': No such file or directory"

(where '14116.1 CX1 CP1-1.nd2 (Log).txt' one of the files of interest).

I'm trying to build a code for cbind-ing multiple tables into a final big table, export them and then use it for stats. Otherwise i'd have to access the hundreds of files individually and copy/paste the column of my interest one at a time. A complete waste of time.

The code I built is:

##read final table backup for formatting
final_table_backup <- read.csv(file = "../Analysis output/final_table_backup.csv", header = T, sep = ",")

##Set working directory
setwd("C:/Users/Sebastian/Desktop/MAP2 co-localisations - (ROI)/CDK5 MAP2/70d/Analysis output/")
##create blank output table
final_table <- ""
##Create file list
file.names <- list.files(path = "14116.1/", pattern = "*.txt")
##cbind all files into final table
for (i in 1:length(file.names)) {
  files <- read.csv(file=file.names[i], header = F, sep = ",")
  final_table <- cbind(final_table_backup, as.character(files[5:32,2]))
##Formatting and exporting
rownames(final_table) <- rownames(final_table_backup)
write.csv(final_table, file="../Analysis output/14116.1output.csv")
And the problem comes in the function for cbind all files into final table. It says the files don't exist, but clearly they do, otherwise the line file.names <- list.files(path = "14116.1/", pattern = "*.txt") wouldn't work, and it does. The output of this line is a list of the 36 names of the .txt files I have in that directory:

[1] "14116.1 CX1 CP1-1.nd2 (Log).txt" "14116.1 CX1 CP1-2.nd2 (Log).txt"
[3] "14116.1 CX1 CP2-1.nd2 (Log).txt" "14116.1 CX1 CP2-2.nd2 (Log).txt"
[5] "14116.1 CX1 CP3-1.nd2 (Log).txt" "14116.1 CX1 CP3-2.nd2 (Log).txt"
And so on...

What am I doing wrong? Does the list.file function creates a list that is not recognisable as "files" by the read.csv function? If so, what's the right function?

I'd really appreciate the help =)

Best regards,

One approach: in this line of yours, state the root directory explicitly and make sure that it is set up as the working directory in R:

final_table_backup <- read.csv(file = "../Analysis output/final_table_backup.csv", header = T, sep = ",")

Also, make sure that all the input and output files are not open in other software.


Ambassador to the humans
You use list.files to get the files in a different folder but then when you use read.csv you just give the file name and not the full path. It didn't know to look in the 14116.1 folder.