-
Notifications
You must be signed in to change notification settings - Fork 18
/
Homework7.R
49 lines (32 loc) · 1.1 KB
/
Homework7.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
rm(list=ls())
setwd("~/Desktop/intro-biocomp/class/Biocomp-Fall2018-181012-Exercise7")
iris <- read.csv("iris.csv", header = T, stringsAsFactors = F)
# 1. function that returns odd rows of any data frame
odd <- function(x){
a <- seq(1, nrow(x),2)
x[a, ]
}
#example
odd(iris)
# 2. repeat a subset of last week, but use functions
# return the number of observations for a given species included in the data set
species <- function(name){
n <- iris[iris$Species == name,]
return(nrow(n))
}
#example
species('setosa')
# return a dataframe for flowers with Sepal.Width greater than a value specified by function
sep.width <- function(measure){
df <- iris[iris$Sepal.Width > measure,]
return(df)
}
#example
sep.width(4)
# write the data for a given species to a comma-deliminated file with the species as name
write.species <- function(file){
n <- iris[iris$Species == file,]
write.csv(n, file = file) #Brittni: This doesn't have the file extension. We wanted to write a file called "setosa.csv". To do so, use "write.csv(n, file = paste(file, ".csv", sep="")". (-0.25)
}
#example
write.species('setosa')