From 5f326aa66bc8f6375a46f96d0c3f5ae46ca1946b Mon Sep 17 00:00:00 2001 From: eml113 Date: Fri, 5 Nov 2021 06:22:14 -0400 Subject: [PATCH] Created R Studio script for Exercise 7 --- Exercise 7 - Erin Lewis.R | 48 ++++++++++++++++++++++++++++++++++++ setosa.csv | 51 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 Exercise 7 - Erin Lewis.R create mode 100644 setosa.csv diff --git a/Exercise 7 - Erin Lewis.R b/Exercise 7 - Erin Lewis.R new file mode 100644 index 0000000..c6a55d7 --- /dev/null +++ b/Exercise 7 - Erin Lewis.R @@ -0,0 +1,48 @@ +#navigate to working directory +setwd('/Users/erinlewis/Desktop/Biocomp_tutorial9/') + +#1. Write R code that replicates the functionality of the head function we used in bash. + +#define a variable with the file to return lines from +exampleFile <- read.csv(file = 'wages.csv') +#wages.csv is used as a test file in this case, any file could be inserted + +#define a variable representing the number of lines to be returned from the top of the indicated file. +n <- seq(1:10) +#in this case, just had n be the typical amount read by bash head function, could be changed + +#replicate bash function using variables +exampleFile[n, ] + + + +## 2. Load the data contained in the provided ‘iris.csv’ file and write R code to do the following things: +data <- read.table(file="iris.csv",header=TRUE, sep=",") + +##print the last 2 rows in the last 2 columns to the R terminal +tail(data, n= c(2,2)) + +##get the number of observations for each species included in the data set +unique(data$Species) +#setosa +nrow(data[data$Species=="setosa",]) +#versicolor +nrow(data[data$Species=="versicolor",]) +#virginica +nrow(data[data$Species=="virginica",]) + +##get rows with Sepal.Width > 3.5 +data[data[,2]>3.5,] + +##write the data for the species setosa to a comma-delimited file names ‘setosa.csv’ +write.table(x = data[data$Species=="setosa",], file="setosa.csv",sep=",",col.names=TRUE) + +##calculate the mean, minimum, and maximum of Petal.Length for observations from virginica +uniquePlant<-data[data$Species=="virginica",] +sortedPlant<-sort(x=uniquePlant$Petal.Length,decreasing = FALSE) +#mean +mean(uniquePlant$Petal.Length,) +#minimum +sortedPlant[1] +#maximum +tail(sortedPlant, n=1) \ No newline at end of file diff --git a/setosa.csv b/setosa.csv new file mode 100644 index 0000000..3c63e48 --- /dev/null +++ b/setosa.csv @@ -0,0 +1,51 @@ +"Sepal.Length","Sepal.Width","Petal.Length","Petal.Width","Species" +"1",5.1,3.5,1.4,0.2,"setosa" +"2",4.9,3,1.4,0.2,"setosa" +"3",4.7,3.2,1.3,0.2,"setosa" +"4",4.6,3.1,1.5,0.2,"setosa" +"5",5,3.6,1.4,0.2,"setosa" +"6",5.4,3.9,1.7,0.4,"setosa" +"7",4.6,3.4,1.4,0.3,"setosa" +"8",5,3.4,1.5,0.2,"setosa" +"9",4.4,2.9,1.4,0.2,"setosa" +"10",4.9,3.1,1.5,0.1,"setosa" +"11",5.4,3.7,1.5,0.2,"setosa" +"12",4.8,3.4,1.6,0.2,"setosa" +"13",4.8,3,1.4,0.1,"setosa" +"14",4.3,3,1.1,0.1,"setosa" +"15",5.8,4,1.2,0.2,"setosa" +"16",5.7,4.4,1.5,0.4,"setosa" +"17",5.4,3.9,1.3,0.4,"setosa" +"18",5.1,3.5,1.4,0.3,"setosa" +"19",5.7,3.8,1.7,0.3,"setosa" +"20",5.1,3.8,1.5,0.3,"setosa" +"21",5.4,3.4,1.7,0.2,"setosa" +"22",5.1,3.7,1.5,0.4,"setosa" +"23",4.6,3.6,1,0.2,"setosa" +"24",5.1,3.3,1.7,0.5,"setosa" +"25",4.8,3.4,1.9,0.2,"setosa" +"26",5,3,1.6,0.2,"setosa" +"27",5,3.4,1.6,0.4,"setosa" +"28",5.2,3.5,1.5,0.2,"setosa" +"29",5.2,3.4,1.4,0.2,"setosa" +"30",4.7,3.2,1.6,0.2,"setosa" +"31",4.8,3.1,1.6,0.2,"setosa" +"32",5.4,3.4,1.5,0.4,"setosa" +"33",5.2,4.1,1.5,0.1,"setosa" +"34",5.5,4.2,1.4,0.2,"setosa" +"35",4.9,3.1,1.5,0.2,"setosa" +"36",5,3.2,1.2,0.2,"setosa" +"37",5.5,3.5,1.3,0.2,"setosa" +"38",4.9,3.6,1.4,0.1,"setosa" +"39",4.4,3,1.3,0.2,"setosa" +"40",5.1,3.4,1.5,0.2,"setosa" +"41",5,3.5,1.3,0.3,"setosa" +"42",4.5,2.3,1.3,0.3,"setosa" +"43",4.4,3.2,1.3,0.2,"setosa" +"44",5,3.5,1.6,0.6,"setosa" +"45",5.1,3.8,1.9,0.4,"setosa" +"46",4.8,3,1.4,0.3,"setosa" +"47",5.1,3.8,1.6,0.2,"setosa" +"48",4.6,3.2,1.4,0.2,"setosa" +"49",5.3,3.7,1.5,0.2,"setosa" +"50",5,3.3,1.4,0.2,"setosa"