I am trying to take two character vectors:
directory <- "specdata"
id <- 1
and read the data in from the file that they would "point" to: ie:
data <- read.table(paste(directory,"\\",id,".csv", sep="")
The problem is in the result of paste and the "\". I am trying to get it to return "specdata\1.csv"
however it returns "specdata\\1.csv"
which is not the same.
To no avail, I have also tried:
"\"
'\\'
'\'
'\'
'\\'
"\"
"\\"
code:
fileNameAndPath <- c(directory,"\",id,".csv")
data <- read.table(fileNameAndPath)
The best way to get the file path in the correct form in R is with the function readClipboard(). It will automatically change a single backslash to a double backslash. Backslashes need to be doubled because a single one is the escape character in R. In other words, a single backslash in an R requires two backslashes.
A path is made up of folder names. If the path is to a file, then the path will ends with a file name. The folders and files of a path are separated by a directory separator (e.g., / or \ ). Different operating systems use different directory separators. In R, the function file.
You should use file.path
instead (it is independent of your platform):
file.path(directory, paste(id, ".csv", sep=""))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With