Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using paste() to construct windows path in R

Tags:

windows

r

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:

  • using a single-slash "\"
  • using single quotes like '\\'
  • using single quotes like '\'
  • changing sep = '\'
  • changing sep = '\\'
  • changing sep = "\"
  • changing sep = "\\"
  • using the c() function first like

code:

fileNameAndPath <- c(directory,"\",id,".csv")
data <- read.table(fileNameAndPath)
like image 920
Bryan Wolfford Avatar asked Sep 29 '12 05:09

Bryan Wolfford


People also ask

How do you set the path in Windows R?

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.

What is a path in R?

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.


1 Answers

You should use file.path instead (it is independent of your platform):

file.path(directory, paste(id, ".csv", sep=""))
like image 77
sgibb Avatar answered Nov 15 '22 19:11

sgibb