forked from timaburton/Biocomp_tutorial9
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTutorial09Answer.R
More file actions
42 lines (34 loc) · 1.31 KB
/
Copy pathTutorial09Answer.R
File metadata and controls
42 lines (34 loc) · 1.31 KB
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
37
38
39
40
41
42
## Problem 1
#define variable with a data file
allfile<-read.table("file",header=TRUE,sep=",",stringsAsFactors=FALSE)
#define variable for number of lines to be returned from the top of data1
returnlines<-allfile[1:n,]
#Test: output should return first 8 lines from wages.csv
allfile<-read.table("wages.csv",header=TRUE,sep=",",stringsAsFactors=FALSE)
returnlines<-allfile[1:8,]
returnlines
## Problem 2
## Load data from iris.csv
iris<-read.table("iris.csv",header=TRUE,sep=",",stringsAsFactors=FALSE)
#Select last two rows in last two columns
output <- iris[, c(4,5)]
output
answer1 <- tail(output, n-2)
answer1
#get the number of observations for each species included in the data set
unique(iris$Species)
sum(iris$Species=="setosa")
sum(iris$Species=="versicolor")
sum(iris$Species=="virginica")
#get rows with Sepal.Width > 3.5
sepalwidth<-iris[,2]
answer3<-iris[sepalwidth>3.5,]
#write the data for the species setosa to a comma-delimited file names 'setosa.csv'
species<-iris[,5]
setosa=iris[species=="setosa",]
write.table(x=setosa,file="setosa.csv",row.names=FALSE,col.names=TRUE,sep=",")
#calculate the mean, minimum, and maximum of Petal.Length for observations from virginica
virginica_data=iris[iris[,5]=="virginica",]
max(virginica_data$Petal.Length)
min(virginica_data$Petal.Length)
mean(virginica_data$Petal.Length)