需要帮助理解涉及R中“…”参数的错误

问题描述 投票:0回答:1
install.packages("data.table")
library("data.table")

pollutantmean <- function(directory, pollutant, id= 1:332){

    ## Create an empty vector of pollutants
    pollutants = c()

    ## Get a list of filenames
    filenames = list.files(directory)

    ## For each .csv file in id
    for (i in id) {

            ## Concatinate the directory and filename
            ## e.g.
            directory = "C:/Users/YUVRAJ/Downloads/rprog_data_specdata"
            filenames = vector("001.csv", "002.csv",...)
            filepath="C:/Users/YUVRAJ/Downloads/rprog_data_specdata/001.csv"
            filepath=paste(directory,"/" , filenames[i], sep="")

            ## read in each file and store it in data
            data = read.csv(filepath, header = TRUE)

            ##Concatinate the vectors from each file of the pollutant('sulfate' or 'nitrate') column to pollutants vector
            pollutants = c(pollutants, data[,pollutant])

    }
    ## Get the mean of the pollutants and remove NA values
    pollutants_mean = mean(pollutants, na.rm=TRUE)

    ## Return the mean 'pollutants_mean'
    pollutants_mean
}

错误消息:

Error in vector("001.csv", "002.csv", ...) : 
  '...' used in an incorrect context
r arguments
1个回答
0
投票

您只能在两个设置中使用...。 1.在编写自己的函数以允许更多用户定义的参数时。 2.将这些用户定义的参数传递给另一个函数时。此外,vector的功能定义不支持...

[使用c创建矢量时

filenames = c("001.csv", "002.csv")

您将删除错误。我不确定能否实现您想要的目标,因为我不清楚。

© www.soinside.com 2019 - 2024. All rights reserved.