Skip to content
Open

Li #12

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions answers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#challenge 1
#codes that replicates the functionality of the head function. This code will return the first x lines of file "wages.csv"
wages = read.csv(file = "wages.csv", header = TRUE)
x <- 10 #x is the number of rows that will be shown in results; replace 10 by any row numbers for other results
wages[1:x,] #return the first 10 lines of file "wages.csv"

#challenge 2
#load file
read.csv(file = "iris.csv", header = TRUE)

#1. print the last 2 rows in the last 2 columns to the R terminal
tail(iris[tail(ncol(iris)-1):ncol(iris)], 2)

#2. get the number of observations for each species included in the data set
unique(iris$Species)
length(which(iris$Species == 'setosa'))
length(which(iris$Species == 'versicolor'))
length(which(iris$Species == 'virginica'))

#3. get rows with Sepal.Width > 3.5
iris[iris$Sepal.Width > 3.5,]

#4. write the data for the species setosa to a comma-delimited file names ‘setosa.csv’
setosa <- iris[iris$Species == "setosa",]
write.table(x=setosa,file="setosa.csv", row.name=FALSE,col.names = TRUE, sep = ",")

#5. calculate the mean, minimum, and maximum of Petal.Length for observations from virginica
virginica = iris[iris$Species == "virginica",]
mean(virginica$Petal.Length)
max(virginica$Petal.Length)
min(virginica$Petal.Length)
51 changes: 51 additions & 0 deletions setosa.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"Sepal.Length","Sepal.Width","Petal.Length","Petal.Width","Species"
5.1,3.5,1.4,0.2,"setosa"
4.9,3,1.4,0.2,"setosa"
4.7,3.2,1.3,0.2,"setosa"
4.6,3.1,1.5,0.2,"setosa"
5,3.6,1.4,0.2,"setosa"
5.4,3.9,1.7,0.4,"setosa"
4.6,3.4,1.4,0.3,"setosa"
5,3.4,1.5,0.2,"setosa"
4.4,2.9,1.4,0.2,"setosa"
4.9,3.1,1.5,0.1,"setosa"
5.4,3.7,1.5,0.2,"setosa"
4.8,3.4,1.6,0.2,"setosa"
4.8,3,1.4,0.1,"setosa"
4.3,3,1.1,0.1,"setosa"
5.8,4,1.2,0.2,"setosa"
5.7,4.4,1.5,0.4,"setosa"
5.4,3.9,1.3,0.4,"setosa"
5.1,3.5,1.4,0.3,"setosa"
5.7,3.8,1.7,0.3,"setosa"
5.1,3.8,1.5,0.3,"setosa"
5.4,3.4,1.7,0.2,"setosa"
5.1,3.7,1.5,0.4,"setosa"
4.6,3.6,1,0.2,"setosa"
5.1,3.3,1.7,0.5,"setosa"
4.8,3.4,1.9,0.2,"setosa"
5,3,1.6,0.2,"setosa"
5,3.4,1.6,0.4,"setosa"
5.2,3.5,1.5,0.2,"setosa"
5.2,3.4,1.4,0.2,"setosa"
4.7,3.2,1.6,0.2,"setosa"
4.8,3.1,1.6,0.2,"setosa"
5.4,3.4,1.5,0.4,"setosa"
5.2,4.1,1.5,0.1,"setosa"
5.5,4.2,1.4,0.2,"setosa"
4.9,3.1,1.5,0.2,"setosa"
5,3.2,1.2,0.2,"setosa"
5.5,3.5,1.3,0.2,"setosa"
4.9,3.6,1.4,0.1,"setosa"
4.4,3,1.3,0.2,"setosa"
5.1,3.4,1.5,0.2,"setosa"
5,3.5,1.3,0.3,"setosa"
4.5,2.3,1.3,0.3,"setosa"
4.4,3.2,1.3,0.2,"setosa"
5,3.5,1.6,0.6,"setosa"
5.1,3.8,1.9,0.4,"setosa"
4.8,3,1.4,0.3,"setosa"
5.1,3.8,1.6,0.2,"setosa"
4.6,3.2,1.4,0.2,"setosa"
5.3,3.7,1.5,0.2,"setosa"
5,3.3,1.4,0.2,"setosa"