Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### Ch 2 Exercise 3 ###########################
- #Step 2 (set working directory)
- getwd()
- #Step 3 (import data)
- ch2_iris_data <- read.csv("C:/Users/benny/Downloads/ch2_iris_data.csv")
- View(ch2_iris_data)
- mydf=ch2_iris_data
- #Step 4 (first scatterplot - add code here when you get to Step 6) petal width v length
- par(mfrow=c(1,2))
- plot(mydf$Petal_Length_mm~mydf$Petal_Width_mm,col=mydf$Species_num,xlab="Petal width (mm)",ylab = "Petal length (mm)") #i titled this scatterplot but it overlapped with the title of both plots so i deleted it
- plot(mydf$Sepal_Length_mm~mydf$Sepal_Width_mm,col=mydf$Species_num,xlab="Sepal width (mm)",ylab = "Sepal length (mm)")
- title("Scatterplots of Petal and Sepal length and width",line = -2,outer = TRUE)
- plot(Petal_Length_mm~Petal_Width_mm,data = mydf,col=mydf$Species_num,main="Scatterplot of Petal width v. length",xlab="Petal width (mm)",ylab = "Petal length (mm)")
- abline(lm(Petal_Length_mm~Petal_Width_mm,data = mydf[which(mydf$Species_num==1),]))
- abline(lm(Petal_Length_mm~Petal_Width_mm,data = mydf[which(mydf$Species_num==2),]),col="green")
- abline(lm(Petal_Length_mm~Petal_Width_mm,data = mydf[which(mydf$Species_num==3),]),col="red") #great success in copy-pasting my code from the sepal plot and editing it to make this code
- #Step 4 (second scatterplot) sepal width v length
- plot(Sepal_Length_mm~Sepal_Width_mm,data = mydf,col=mydf$Species_num,main="Scatterplot of Sepal width v. length",xlab="Sepal width (mm)",ylab = "Sepal length (mm)") #code that makes plot
- abline(lm(Sepal_Length_mm~Sepal_Width_mm,data = mydf[which(mydf$Species_num==1),])) #makes line of regression for first species
- abline(lm(Sepal_Length_mm~Sepal_Width_mm,data = mydf[which(mydf$Species_num==2),]),col="green") #so cool seeing "green" light up that color
- abline(lm(Sepal_Length_mm~Sepal_Width_mm,data = mydf[which(mydf$Species_num==3),]),col="red")
- #Step 5 (add legend to the second scatterplot)
- legend("topleft",legend = c("I. setosa","I. versicolor","I. verginica"),col = c("black","green","red"),pch = 1,lty = 1)
- #Step 7 (create a new data frame)
- #iris_data$Petal_Length_mm #prints a single column in a df //petal then species
- x=c(mydf$Petal_Width_mm)
- y=c(mydf$Species_name)
- pw_df=data.frame(x,y)
- #Step 8 (unstack)
- unstack_df=unstack(pw_df)
- #Step 9 (output data frame as .csv)
- write.csv(pw_df,file = "unstacked")
Advertisement
Add Comment
Please, Sign In to add comment