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

用户123

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

我的功能:

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""
恩索

尝试以下功能。它运作良好:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章