【问题标题】:Paste function to add backslash in r粘贴函数以在 r 中添加反斜杠
【发布时间】:2020-03-14 14:09:38
【问题描述】:

这可能是一个非常特殊的请求,但我想在 R 中的每个文件之前的 file.path 字符串的末尾粘贴一个反斜杠 (\)。出于某种原因,R 不喜欢该函数:sep=" \",我不知道为什么......任何帮助将不胜感激

# Image files 
files <- c("image1.bmp", "image2.bmp", "image3.bmp", "image4.bmp", "image5.bmp")

# Pasting file paths and file names 
file.paths <- paste("C:/Users/John/Desktop/images/", files, sep="\")

# Desired output 
C:/Users/John/Desktop/images\image1.bmp
C:/Users/John/Desktop/images\image2.bmp
C:/Users/John/Desktop/images\image3.bmp
C:/Users/John/Desktop/images\image4.bmp
C:/Users/John/Desktop/images\image5.bmp

【问题讨论】:

  • 试试 \\.可能会显示为双反斜杠,但实际上它是一个,如果你 cat
  • 另外,在指定 sep 时,您需要使用 paste(),而不是 paste0()
  • paste0 没有 sep 参数。你的意思可能是paste("C:/Users/John/Desktop/images/", files, sep="\\")
  • 是的,粘贴是正确的功能
  • 正如@arg0naut91 已经提到的,paste("C:/Users/John/Desktop/images", files, sep="\\") 不适合你吗?

标签: r paste names


【解决方案1】:

\\ 可以工作,但它在控制台输出中显示为双反斜杠,但是您不能使用messagecat 来查看它的自然外观。

file.paths <- paste0("C:/Users/John/Desktop/images\\" , files)

message(file.paths[1])
cat(file.paths[1])

fileConn<-file("outputtest.txt")
writeLines(file.paths, fileConn)
close(fileConn)

保存在文本文件输出中:

【讨论】:

    猜你喜欢
    • 2018-01-03
    • 2011-04-30
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 2020-10-05
    • 2019-07-04
    • 2016-07-09
    • 1970-01-01
    相关资源
    最近更新 更多