R 语言程序设计
Data
The zip file containing the data can be downloaded here:
- specdata.zip [2.4MB]
The zip file contains 332 comma-separated-value (CSV) files containing pollution monitoring data for fine particulate matter (PM) air pollution at 332 locations in the United States. Each file contains data from a single monitor and the ID number for each monitor is contained in the file name. For example, data for monitor 200 is contained in the file "200.csv". Each file contains three variables:
- Date: the date of the observation in YYYY-MM-DD format (year-month-day)
- sulfate: the level of sulfate PM in the air on that date (measured in micrograms per cubic meter)
- nitrate: the level of nitrate PM in the air on that date (measured in micrograms per cubic meter)
For this programming assignment you will need to unzip this file and create the directory 'specdata'. Once you have unzipped the zip file, do not make any modifications to the files in the 'specdata' directory. In each file you'll notice that there are many days where either sulfate or nitrate (or both) are missing (coded as NA). This is common with air pollution monitoring data in the United States.
Part 1
Write a function named 'pollutantmean' that calculates the mean of a pollutant (sulfate or nitrate) across a specified list of monitors. The function 'pollutantmean' takes three arguments: 'directory', 'pollutant', and 'id'. Given a vector monitor ID numbers, 'pollutantmean' reads that monitors' particulate matter data from the directory specified in the 'directory' argument and returns the mean of the pollutant across all of the monitors, ignoring any missing values coded as NA. A prototype of the function is as follows
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)
## NOTE: Do not round the result!
}
You can see some example output from this function. The function that you write should be able to match this output. Please save your code to a file named pollutantmean.R.
pollutantmean <- function(directory, pollutant, id = 1:332){
tempsum <- 0
templen <- 0
for(i in id ){
fid <- sprintf("%03d",i)
filename <- paste(directory,"/",fid,".csv",sep="")
dat <- read.csv(filename)
src <- dat[pollutant]
src <- na.omit(src) # omit NA
tempsum <- tempsum + sum(src)
templen <- templen + dim(src)[1]
}
if(templen >0 ){
pollutantmean <- tempsum / templen
}
pollutantmean
}
Part 2
Write a function that reads a directory full of files and reports the number of completely observed cases in each data file. The function should return a data frame where the first column is the name of the file and the second column is the number of complete cases. A prototype of this function follows
complete <- function(directory, id = 1:332) {
## 'directory' is a character vector of length 1 indicating
## the location of the CSV files ## 'id' is an integer vector indicating the monitor ID numbers
## to be used ## Return a data frame of the form:
## id nobs
## 1 117
## 2 1041
## ...
## where 'id' is the monitor ID number and 'nobs' is the
## number of complete cases
}
You can see some example output from this function. The function that you write should be able to match this output. Please save your code to a file named complete.R. To run the submit script for this part, make sure your working directory has the file complete.R in it.
complete <- function(directory, id = 1:332){
nobs <- NULL
for(i in id){
fid <- sprintf("%03d",i)
filename <- paste(directory,"/",fid,".csv",sep="")
dat <- read.csv(filename)
src <- na.omit(dat) # omit NA
nobs <- c(nobs,dim(src)[1])
}
complete <- data.frame(id,nobs)
}
Part 3
Write a function that takes a directory of data files and a threshold for complete cases and calculates the correlation between sulfate and nitrate for monitor locations where the number of completely observed cases (on all variables) is greater than the threshold. The function should return a vector of correlations for the monitors that meet the threshold requirement. If no monitors meet the threshold requirement, then the function should return a numeric vector of length 0. A prototype of this function follows
corr <- function(directory, threshold = 0) {
## 'directory' is a character vector of length 1 indicating
## the location of the CSV files ## 'threshold' is a numeric vector of length 1 indicating the
## number of completely observed observations (on all
## variables) required to compute the correlation between
## nitrate and sulfate; the default is 0 ## Return a numeric vector of correlations
## NOTE: Do not round the result!
}
For this function you will need to use the 'cor' function in R which calculates the correlation between two vectors. Please read the help page for this function via '?cor' and make sure that you know how to use it.
You can see some example output from this function. The function that you write should be able to match this output. Please save your code to a file named corr.R. To run the submit script for this part, make sure your working directory has the file corr.R in it.
corr <- function(directory, threshold = 0) {
corr.list <- NULL
id <- 1:332
dat <- NULL
for(i in id){
fid <- sprintf("%03d",i)
filename <- paste(directory,"/",fid,".csv",sep="")
dat <- read.csv(filename)
src <- na.omit(dat) # omit NA'
dat <- src
len <- length(dat$ID);
if(len > threshold && threshold >=0 ){
corr.re <- cor(dat$sulfate, dat$nitrate)
corr.list=c(corr.list, corr.re)
}
}
corr.list
}
R 语言程序设计的更多相关文章
- R语言介绍
R语言简介 R语言是一种为统计计算和图形显示而设计的语言环境,是贝尔实验室(Bell Laboratories)的Rick Becker.John Chambers和Allan Wilks开发的S语言 ...
- 统计计算与R语言的资料汇总(截止2016年12月)
本文在Creative Commons许可证下发布. 在fedora Linux上断断续续使用R语言过了9年后,发现R语言在国内用的人逐渐多了起来.由于工作原因,直到今年暑假一个赴京工作的机会与一位统 ...
- 机器学习 1、R语言
R语言 R是用于统计分析.绘图的语言和操作环境.R是属于GNU系统的一个自由.免费.源代码开放的软件,它是一个用于统计计算和统计制图的优秀工具. 特点介绍 •主要用于统计分析.绘图.数据挖掘 •R内置 ...
- 【R语言系列】R语言初识及安装
一.R是什么 R语言是由新西兰奥克兰大学的Ross Ihaka和Robert Gentleman两个人共同发明. 其词法和语法分别源自Schema和S语言. R定义:一个能够自由幼小的用于统计计算和绘 ...
- # C语言程序设计第一次作业1234
---恢复内容开始--- C语言程序设计第一次作业 1.求圆面积和周长 输入圆的半径,计算圆的周长和面积 (1)流程图 (2)测试数据及运行结果 测试数据r=3 运行结果 2.判断闰年 输入一个四位年 ...
- 比较分析C++、Java、Python、R语言的面向对象特征,这些特征如何实现的?有什么相同点?
一门课的课后题答案,在这里备份一下: 面向对象程序设计语言 – 比较分析C++.Java.Python.R语言的面向对象特征,这些特征如何实现的?有什么相同点? C++ 语言的面向对象特征: 对象模 ...
- R语言手册
在R的官方教程里是这么给R下注解的:一个数据分析和图形显示的程序设计环境(A system for data analysis and visualization which is built bas ...
- 《数据挖掘:R语言实战》
<数据挖掘:R语言实战> 基本信息 作者: 黄文 王正林 丛书名: 大数据时代的R语言 出版社:电子工业出版社 ISBN:9787121231223 上架时间:2014-6-6 出版 ...
- 【R】R语言常用函数
R语言常用函数 基本 一.数据管理vector:向量 numeric:数值型向量 logical:逻辑型向量character:字符型向量 list:列表 data.frame:数据框c:连接为向量或 ...
随机推荐
- 搜索表头的例子-jqueryEasyUi
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- SQL Server发布订阅功能研究
前提: 发布订阅只能是同一个内网的机器上才能使用,其实这个可以用配置管理器的别名功能设置之后就可以了,外网的也能通过这样的方式来搞. 配置过程参考老D的文章:http://www.cnblogs.co ...
- dedecms /member/uploads_edit.php SQL Injection Vul
catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 Dedecms 5.3版本下的member/uploads_edit.p ...
- 遍历map集合
for(var key in map){ if(data.hasOwnProperty(key)){ var value = map[key]; } }
- 加州大学伯克利分校Stat2.3x Inference 统计推断学习笔记: FINAL
Stat2.3x Inference(统计推断)课程由加州大学伯克利分校(University of California, Berkeley)于2014年在edX平台讲授. PDF笔记下载(Acad ...
- Linux 下的常用工具
Useful Linux Utilities (This article is under constant construction) ssh 相关文章 How To Change OpenSSH ...
- rpm包安装过程中依赖问题“libc.so.6 is needed by XXX”解决方法
rpm包安装过程中依赖问题"libc.so.6 is needed by XXX"解决方法 折腾了几天,终于搞定了CentOS上的Canon LBP2900打印机驱动.中间遇到了一 ...
- UVa 11889 Benefit(数论)
题目链接: 传送门 Benefit Time Limit: 5000MS Memory Limit: 32768 KB Description Recently Yaghoub is play ...
- Java Code Examples for javax.servlet.http.Part
http://www.programcreek.com/java-api-examples/index.php?api=javax.servlet.http.Part The following ar ...
- linux basis --- common commands
switch to root : sudo su switch to users : su god(user name) set root password : sudo passwd root ch ...