read.csv显式路径字符串中的变量

时间:2018-08-20 23:43:14

标签: r

问题:

如何将变量插入嵌套在read.csv函数内部的显式目录路径中?尝试cat和sprintf无效,可能是因为该函数嵌套了。

R代码:

    mainDir <- "C:/Users/"
    subDir <- "G"

    if (file.exists(subDir)) {
        userName <- "John"
    } else {
        userName <- "Mike"
    }

    sampleData <- read.csv("C:/Users/", userName, "/Google Drive/data/sampleData.csv")

1 个答案:

答案 0 :(得分:0)

鉴于上述@Dan Y和@Edward Carney的建议:

pathStart <- "C:/Users/"

if (file.exists("C:/Users/John")) {
    pathMiddle <- "John"
} else {
    pathMiddle <- "Mike"
}

pathEnd <- "/Google Drive/data/sampleData.csv"

sampleData <- read.csv(paste0(pathStart, pathMiddle, pathEnd, sep=''))