Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Load the gdata package
- library(gdata)
- library(readxl)
- # Import the spreadsheet: att
- at<-read_excel("attendance.xls")
- att<-read.xls("attendance.xls")
- # Print the column names
- colnames(att)
- # Print the first 6 rows
- head(att,n=6)
- # Print the last 6 rows
- tail(att,n=6)
- # Print the structure
- str(att)
- dim(att)
- # Create remove
- remove<-c(3,56:59)
- # Create att2
- att2<-att[-remove,]
- # Create remove
- remove<-c(3,5,7,9,11,13,15,17)
- # Create att3
- att3<-att2[,-remove]
- library(dplyr)
- # Subset just elementary schools: att_elem
- att_elem<-att3[,c(1,6,7)]
- # Subset just secondary schools: att_sec
- att_sec<-att3[,c(1,8,9)]
- # Subset all schools: att4
- att4<-att3[,c(1:5)]
- # Define cnames vector
- cnames <- c("state", "avg_attend_pct", "avg_hr_per_day", "avg_day_per_yr", "avg_hr_per_yr")
- # Assign column names of att4
- colnames(att4) <- cnames
- # Remove first two rows of att4: att5
- att5<-att4[-c(1,2),]
- # View the names of att5
- colnames(att5)
- glimpse(att5)
- # Remove all periods in state column
- att5$state<-str_replace_all(att5$state,"\\.","")
- # Remove white space around state names
- att5$state<-str_trim(att5$state)
- # View the head of att5
- head(att5)
- # Change columns to numeric using dplyr
- example <- mutate_each(att5, funs(as.numeric), -state)
- # Define vector containing numerical columns: cols
- # Change columns to numeric using dplyr
- dim(att5)
- i<-c(1:5)
- cols<-c(2:5)
- # Use sapply to coerce cols to numeric
- att5[, cols] <- sapply(att5[,cols],as.numeric)
Add Comment
Please, Sign In to add comment