Skip to content
Open
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
35 changes: 35 additions & 0 deletions Tutorial9_Answers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Tutorial 9, Exercise 7

# Problem 1

#Set working directory
setwd("~/Desktop/Fall-2021/Biocomputing/Tutorial9/Biocomp_tutorial9")
#Define a variable with the file to return lines from
exampleFile <- read.table("iris.csv",header=TRUE,sep=",")
#Define variable representing the number of lines to be returned from the top of the file (can be any number)
numLines = 8
#Select certain number of lines from the top of the file (use square bracket indexing)
results <- exampleFile[1:numLines, ]
#Print file content to terminal
results

# Problem 2

# Load data from 'iris.csv'
irisFile <- read.table("iris.csv",header=TRUE,sep=",")
# Print the last two rows in the last two columns to the R terminal
irisFile[149:150,4:5]
# Get the number of observations for each species in the data set
sum(irisFile$Species=='setosa')
sum(irisFile$Species=='versicolor')
sum(irisFile$Species=='virginica')
# Get rows with Sepal.Width > 3.5
irisFile[irisFile$Sepal.Width>3.5,]
# Write the data for the species setosa to a comma-delimited file named 'setosa.csv'
setosaData<-irisFile[irisFile$Species=='setosa',]
write.csv(setosaData,file = 'setosa.csv',row.names=FALSE)
# Calculate the mean, minimum, and maximum of Petal.Length for observations from virginica
mean(irisFile$Petal.Length[irisFile$Species=='virginica'])
min(irisFile$Petal.Length[irisFile$Species=='virginica'])
max(irisFile$Petal.Length[irisFile$Species=='virginica'])