如何在R中连接三个或更多字符串

时间:2013-05-28 08:52:50

标签: r

在PHP中,我可以使用点来粘贴字符串:

$path = "/path/to/directory/";
$extension =".txt";
$filecounter = $i; // from some loop for creating multiple files
$file = $path . "filename_". $filecounter . $extension;

我怎样才能在R中做类似的事情?

path <- "/path/to/directory/"
extension <- ".txt"
filecounter <- i
file <- paste(path, paste(paste("filename", $filecounter, sep =""), extension, sep =""), sep ="")
对于这么简单的任务来说,

似乎需要额外的打字。

5 个答案:

答案 0 :(得分:6)

您还可以使用file.path以与平台无关的方式构建组件文件的路径。

path = "/path/to/directory/"
file = "file.txt"

file.path(path,file)

答案 1 :(得分:4)

在最近的R版本中,您可以使用以下命令保存一些输入:

paste0(path, "file.txt")

paste0(...)相当于paste(..., sep=""),效率稍高。

答案 2 :(得分:4)

您应该使用file.pathpaste,因为它快速且与平台无关。 (参见tools::file_exttools::file_path_sans_ext),但下面是一个小功能,适用于可能派上用场的Linux或Mac系统。我不经常使用它,因为如果我共享我的代码,我要么必须向用户提供此功能或包含它的个人包,所以我通常最终编辑代码以使用file.path并且它转为了不开始使用这个功能而做的工作少了。

> fpath(path, "filename", extension)
[1] "/path/to/directory/filename.txt"

#' create a filepath
#'
#' This just pastes together \code{dir} and \code{file} and optionally 
#' \code{ext}.  It is intended for Mac or Linux systems. 
#' @param dir character string of directory (with or without trailing slash)
#' @param file character string of filename (with or without extension)
#' @param ext Optional. character file extension. (with or without leading dot)
#' @return character string representing a filepath
#' @examples
#' fpath("path/to", "file", "csv")
#' fpath("path/to/", "file.csv", ".csv") 
#' #knows not to duplicatate the "/" or the ".csv"
#' fpath('path/to', 'file.csv')
#' fpath("file", ext="csv") #knows that dir is missing
#' fpath("", "file", "txt") # no leading forward slash if dir == ""
#' @export
fpath <- function(dir, file, ext) {
    lchar <- function(x) substr(x, nchar(x), nchar(x)) #last char of string
    fl <- if (missing(file)) {
      dir
    } else {
        if (missing(dir) || dir == "") {
            file
        } else {
            dir <- if (substr(dir, nchar(dir), nchar(dir)) != "/") { 
                paste(dir, "/", sep="") 
            } else dir
            file <- gsub("^/+", "", file) # remove leading forward slashes from file names
            paste(dir, file, sep="")
            #TODO: should figure out how to throw an error (or allow it to work) if called like 
            #      fpath("ftp:/", "/ftp.domain.com") or fpath('http:/', '/www.domain.com')
        }
    }
    if (lchar(fl) == "/") stop("'file' should not end with a forward slash")
    if (!missing(ext)) {
        ext <- if (substr(ext, 1, 1) != ".") {
            paste(".", ext, sep="")
        } else ext
        if (substr(fl, nchar(fl) - nchar(ext) + 1, nchar(fl)) != ext) {
            fl <- paste(fl, ext, sep="")
        }
    }
    fl
}

答案 3 :(得分:3)

如果你想要看起来像PHP这样的 lot 你可以定义你自己的中缀运算符并将其设置为paste()

"%.%" <- paste
"a" %.% "b" %.% "c" %.% "d"
## [1] "a b c d"

当然,如果您想要连接而不是以空格分隔的字符串,则可以使用paste0

答案 4 :(得分:2)

或替代paste0

sprintf("%s/%s", path, 'file.txt')

对于字符串中的更多条目,@ BenBolker建议:

sprintf("%s/%s.%s", path, file, extension)