The R statement data( iris ) loads the built-in R data set iris . This dataset h
ID: 3665003 • Letter: T
Question
The R statement data(iris) loads the built-in R data set iris. This dataset has 150 rows and 5 columns. The 5 column names are
"Sepal.Length","Sepal.Width","Petal.Length","Petal.Width" and "Species"Write R code(s) to find the average values of Sepal.Length (the first column) for three species “setosa”, “versicolor”, and “virginica” (the fifth column), respectively.
The returned results should be as follows:
setosaver
sicolor
virginica
5.006
5.936
6.588
COS-206-data.csv is a text file that contains comma-separated values (CSV).
Write R code(s) to Read the contents of COS-206-data.csv and store them in a data frame calleddata.
setosaver
sicolor
virginica
5.006
5.936
6.588
Explanation / Answer
For task 1 script is as follows
data(iris)
//extracting the rows from dataframe for species setosa
setosa<-iris[iris$Species=='setosa',]
//storing the sepal lengths of all setosa specie in a different variable
setosa.sl<-setosa$Sepal.Length
versicolor<-iris[iris$Species=='versicolor',]
versicolor.sl=versicolor$Sepal.Length
virginica<-iris[iris$Species=='virginica',]
virginica.sl<-virginica$Sepal.Length
avgSetosa<-c("setosaver",mean(setosa.sl))
avgVersiColor<-c("sicolor",mean(versicolor.sl))
avgVirginica<-c("virginica",mean(virginica.sl))
//Creating a data frame with 3 vectors of heading and average sepal length of mentioned species
avgSepalLength<-data.frame(avgSetosa,avgVersiColor,avgVirginica)
//Displaying the data frame with the results
avgSepalLength
Task 2
First set your working directory to the place where your file is stored in your system
for setting up wd use setwd(file location) ,where file location is the address of your csv files directory
after that just read the csv using read.csv in a data named dataframe.
data=read.csv("COS-206-data.csv ")
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.