From de16d7fba372f1d7560320621eef8ad900da1632 Mon Sep 17 00:00:00 2001 From: Lan Li Date: Thu, 4 Nov 2021 14:21:19 -0400 Subject: [PATCH] lan submission --- answers.R | 31 +++++++++++++++++++++++++++++++ setosa.csv | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 answers.R create mode 100644 setosa.csv diff --git a/answers.R b/answers.R new file mode 100644 index 0000000..6af7677 --- /dev/null +++ b/answers.R @@ -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) \ No newline at end of file diff --git a/setosa.csv b/setosa.csv new file mode 100644 index 0000000..2f03ff3 --- /dev/null +++ b/setosa.csv @@ -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"