-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solution
- Loading branch information
Rajesh Subramanian
committed
Sep 13, 2014
1 parent
d4965a0
commit 920e225
Showing
3 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
**/.Rhistory | ||
**/.RHistory | ||
**/.RData | ||
**/.Rdata | ||
**/specdata/* |
26 changes: 26 additions & 0 deletions
26
programming-with-r/assignments/assignment-1-air-pollution/pollutantmean.R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
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) | ||
|
||
allFiles <- list.files(path = directory, full.names = TRUE) | ||
selectedData <- data.frame() | ||
for (i in id) { | ||
selectedData <- rbind(selectedData, read.csv(allFiles[i])) | ||
} | ||
if (pollutant == 'sulfate') { | ||
mean(selectedData$sulfate, na.rm = TRUE) | ||
} else if (pollutant == 'nitrate') { | ||
mean(selectedData$nitrate, na.rm = TRUE) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
myfunction <- function() { | ||
x <- rnorm(100) | ||
mean(x) | ||
} | ||
|
||
second <- function(x) { | ||
x + rnorm(length(x)) | ||
} |