xeris-s_lair

ch 2 exercise 3

Nov 11th, 2025 (edited)
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.33 KB | None | 0 0
  1. ### Ch 2 Exercise 3 ###########################
  2.  
  3. #Step 2 (set working directory)
  4. getwd()
  5.  
  6. #Step 3 (import data)
  7. ch2_iris_data <- read.csv("C:/Users/benny/Downloads/ch2_iris_data.csv")
  8. View(ch2_iris_data)
  9. mydf=ch2_iris_data
  10.  
  11. #Step 4 (first scatterplot - add code here when you get to Step 6) petal width v length
  12. par(mfrow=c(1,2))
  13. 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
  14. plot(mydf$Sepal_Length_mm~mydf$Sepal_Width_mm,col=mydf$Species_num,xlab="Sepal width (mm)",ylab = "Sepal length (mm)")
  15. title("Scatterplots of Petal and Sepal length and width",line = -2,outer = TRUE)
  16.  
  17. 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)")
  18. abline(lm(Petal_Length_mm~Petal_Width_mm,data = mydf[which(mydf$Species_num==1),]))
  19. abline(lm(Petal_Length_mm~Petal_Width_mm,data = mydf[which(mydf$Species_num==2),]),col="green")
  20. 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
  21.  
  22.  
  23. #Step 4 (second scatterplot) sepal width v length
  24. 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
  25. abline(lm(Sepal_Length_mm~Sepal_Width_mm,data = mydf[which(mydf$Species_num==1),])) #makes line of regression for first species
  26. 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
  27. abline(lm(Sepal_Length_mm~Sepal_Width_mm,data = mydf[which(mydf$Species_num==3),]),col="red")
  28.  
  29. #Step 5 (add legend to the second scatterplot)
  30. legend("topleft",legend = c("I. setosa","I. versicolor","I. verginica"),col = c("black","green","red"),pch = 1,lty = 1)
  31.  
  32. #Step 7 (create a new data frame)
  33.   #iris_data$Petal_Length_mm #prints a single column in a df //petal then species
  34. x=c(mydf$Petal_Width_mm)
  35. y=c(mydf$Species_name)
  36. pw_df=data.frame(x,y)
  37.  
  38. #Step 8 (unstack)
  39. unstack_df=unstack(pw_df)
  40.  
  41. #Step 9 (output data frame as .csv)
  42. write.csv(pw_df,file = "unstacked")
  43.  
Advertisement
Add Comment
Please, Sign In to add comment