首页 文章

在R中,如何将文本字符串中的项目替换为其他项目?

提问于
浏览
3

我是R的初学者 .

我经常使用setwd()切换工作目录 . 我在Windows上,地址有反斜杠,如:C:\ Users \ myname \ Documents,但R使用正斜杠,如下所示:

> getwd()
[1] "C:/Users/myname/Documents"

通常在使用setwd()更改目录时,我发现自己手动从Windows资源管理器中复制粘贴目录的地址,将其粘贴到setwd()命令中并手动将反斜杠更改为正斜杠 .

是否有一个我可以使用的命令将文本字符串中的反斜杠替换为正斜杠?

谢谢!

2 回答

  • 3

    没有内置,但我也被Windows路径烦恼:

    ## Frist Install this
    library(devtools)
    install_github('slidify', 'ramnathv', ref = 'dev')
    install_github('slidifyLibraries', 'ramnathv', ref = 'dev')
    install_github("reports", "trinker")
    
    ## Then use this:
    library(reports)
    WP()
    

    这代表Windows路径,可以从剪贴板中读取并更正斜杠 . 它将正确的版本复制回剪贴板 . 最终,当滑动进入CRAN时,这个版本的reports将进入CRAN .

  • 3

    你可以使用这样的事实:如果你 "C:\mydir" 文本被 "C:\\mydir" 读入 .

    您可以使用 file('clipboard') 访问剪贴板中保存的内容 .

    # something like the following will work
    scan(file('clipboard'), what = 'character')
    
    # and a function that will read and coerce the clipboard
    scanclip <- function(){
      scan(file('clipboard'), what = 'character', quiet= TRUE)}
    # so you can use
    setwd(scanclip())
    

相关问题