Guest User

Untitled

a guest
Jun 17th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. # Load the gdata package
  2. library(gdata)
  3. library(readxl)
  4. # Import the spreadsheet: att
  5. at<-read_excel("attendance.xls")
  6. att<-read.xls("attendance.xls")
  7.  
  8. # Print the column names
  9. colnames(att)
  10.  
  11. # Print the first 6 rows
  12. head(att,n=6)
  13.  
  14. # Print the last 6 rows
  15. tail(att,n=6)
  16.  
  17. # Print the structure
  18. str(att)
  19. dim(att)
  20.  
  21. # Create remove
  22. remove<-c(3,56:59)
  23.  
  24. # Create att2
  25. att2<-att[-remove,]
  26.  
  27. # Create remove
  28. remove<-c(3,5,7,9,11,13,15,17)
  29.  
  30. # Create att3
  31. att3<-att2[,-remove]
  32.  
  33. library(dplyr)
  34. # Subset just elementary schools: att_elem
  35. att_elem<-att3[,c(1,6,7)]
  36.  
  37. # Subset just secondary schools: att_sec
  38. att_sec<-att3[,c(1,8,9)]
  39.  
  40. # Subset all schools: att4
  41. att4<-att3[,c(1:5)]
  42.  
  43. # Define cnames vector
  44. cnames <- c("state", "avg_attend_pct", "avg_hr_per_day", "avg_day_per_yr", "avg_hr_per_yr")
  45.  
  46. # Assign column names of att4
  47. colnames(att4) <- cnames
  48.  
  49. # Remove first two rows of att4: att5
  50. att5<-att4[-c(1,2),]
  51.  
  52. # View the names of att5
  53. colnames(att5)
  54.  
  55. glimpse(att5)
  56.  
  57. # Remove all periods in state column
  58. att5$state<-str_replace_all(att5$state,"\\.","")
  59.  
  60. # Remove white space around state names
  61. att5$state<-str_trim(att5$state)
  62.  
  63. # View the head of att5
  64. head(att5)
  65.  
  66. # Change columns to numeric using dplyr
  67. example <- mutate_each(att5, funs(as.numeric), -state)
  68.  
  69. # Define vector containing numerical columns: cols
  70.  
  71. # Change columns to numeric using dplyr
  72. dim(att5)
  73. i<-c(1:5)
  74. cols<-c(2:5)
  75. # Use sapply to coerce cols to numeric
  76. att5[, cols] <- sapply(att5[,cols],as.numeric)
Add Comment
Please, Sign In to add comment