如何将linux目录路径传递给函数

时间:2014-09-15 05:36:02

标签: r

我检查了类似的线程并进行了相应的尝试,但仍面临问题。

我的功能:

pollutantmean("/root/specdata","","")
pollutantmean <- function(directory, pollutant, id = 1:332) {

    ## Get a list of filenames
    filenames <- list.files(path=directory, pattern="*.csv")

    ## Initialize a vector to hold values
    vals <- vector()

    ## Loop over the passed id's
    for(i in id) {

        ## Pad the i to create a filename
        filename <- sprintf("%03d.csv", i)
        filepath <- paste(directory, filename, sep="/")

        ## Load the data
        data <- read.csv(filepath)

        ## Select our column
        d <- data[,pollutant]

        ## Ignore NAs
        d <- d[!is.na(d)]

        ## append to our vector
        vals <- c(vals, d)
    }

    ## Return the value rounded to 3 dec places
    round(mean(vals), 3)
}

错误:

pollutantmean <- function("/root/specdata", "nitrate", id = 1:332) {
## Error: unexpected string constant in "pollutantmean <- function("/root/specdata""

1 个答案:

答案 0 :(得分:1)

尝试以下功能。效果很好:

dirfn = function(dirname){
    print(getwd())
    setwd(dirname)
    print(getwd())
}