【发布时间】:2016-02-10 18:01:10
【问题描述】:
这是我第一次尝试在 R 中导入多个 CSV 文件并解决这部分作业,使用一些 csv 文件来计算硫酸盐和硝酸盐的平均值。我在 stackoverflow 和其他网站中搜索了答案,但我无法根据此处关于该主题的问题中所说的内容来解决该问题。我也是 R 编程新手。
如果它有用: R版本是3.2.1 Mac OS X 版本 10.7.5
我在 Coursera 中有一个作业,我有 332 个 CSV 文件,我必须计算污染物的平均值。
文件下载链接:https://d396qusza40orc.cloudfront.net/rprog%2Fdata%2Fspecdata.zip
作业第 1 部分:
编写一个名为“pollutantmean”的函数,用于计算指定监视器列表中污染物(硫酸盐或硝酸盐)的平均值。函数“pollutantmean”接受三个参数:“directory”、“pollutant”和“id”。给定一个向量监视器 ID 号,“pollutantmean”从“目录”参数中指定的目录中读取该监视器的颗粒物数据,并返回所有监视器中污染物的平均值,忽略编码为 NA 的任何缺失值。
函数原型:
pollutantmean <- function(directory, pollutant, id = 1:332) {
## 'directory' is a character vector of length 1 indicating
## the location of the CSV files
## 'pollutant' is a character vector of length 1 indicating
## the name of the pollutant for which we will calculate the
## mean; either "sulfate" or "nitrate".
## 'id' is an integer vector indicating the monitor ID numbers
## to be used
## Return the mean of the pollutant across all monitors list
## in the 'id' vector (ignoring NA values)
我的结果应该是:
source("pollutantmean.R")
pollutantmean("specdata", "sulfate", 1:10)
## [1] 4.064
pollutantmean("specdata", "nitrate", 70:72)
## [1] 1.706
pollutantmean("specdata", "nitrate", 23)
## [1] 1.281
我已经创建了我的工作目录,这是我无法更进一步的。
> setwd("~/Desktop/Coursera /R_Prog")
> getwd()
[1] "/Users/amandamariamcharpinel/Desktop/Coursera /R_Prog"
> dir()
[1] "specdata" "specdata.zip"
> directory <- "specdata"
> filelist <- list.files(directory, full.names=TRUE)
> pollutantmean<-function(directory, pollutant, id=1:332)
每当我尝试使用 F1 Error in file(file, "rt") : not possible to open一个连接另外:警告消息:在文件中(文件,“rt”):无法打开文件'nameofile.csv':没有这样的文件或目录 当我使用命令 read.table(filechoose(), header=TRUE) 时,除第一个文件 (001.csv) 外的所有文件都有效,它显示 Error in scan (file, what, nmax, sep, dec , quote, skip, nlines, na.strings, : 第 1 行没有 7 个元素 当我尝试 sapply(filelist, read.csv) 出现同样的错误。当我对“specdata”使用 read.csv、sapply 或 lapply 时,错误是 read.table 中的错误(file = file, header = header, sep = sep, quote = quote,: no lines available in input 虽然我在“specdata”文件中有所有 332.csv 文件。
我希望我发布了可重复练习所需的所有内容。如果还有什么需要,请告诉我。
谢谢!
【问题讨论】:
-
您可能需要阅读open letter to students with homework problems 并考虑修改或删除您的问题。
-
好吧,我试图根据 stackoverflow 中关于作业的其他问题解决我在作业中遇到的错误,但我无法根据其他问题的答案解决问题那个任务。这就是我在这里问这个问题的原因。