使用 paste() 在 R 中构建 Windows 路径 [英] using paste() to construct windows path in R

查看:19
本文介绍了使用 paste() 在 R 中构建 Windows 路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用两个字符向量:

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="")

问题出在粘贴和\"的结果上.我试图让它返回 "specdata\1.csv" 但是它返回 "specdata\\1.csv" 这是不一样的.

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.

无果,我也试过:

  • 使用单斜线 "\"
  • 使用单引号,如 '\\'
  • 使用单引号,如 '\'
  • 改变sep = '\'
  • 更改 sep = '\\'
  • 更改 sep = "\"
  • 更改 sep = "\\"
  • 首先使用 c() 函数

代码:

fileNameAndPath <- c(directory,"\",id,".csv")
data <- read.table(fileNameAndPath)

推荐答案

你应该使用 file.path 代替(它独立于你的平台):

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

file.path(directory, paste(id, ".csv", sep=""))

这篇关于使用 paste() 在 R 中构建 Windows 路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆