Read xls files using gdata package in R

1 min read

Openxlsx package can read xlsx files, but not xls files. To read xlsx files that contain time records for the online course videos, gdata package should be used.

filename <- dir(pattern = "*.xls")
results <- data.frame(row.names = c("click_times","times","class"))
for (f in filename) {
  all_data <- read.xls(f)
  my_data <- subset(all_data,all_data[2]=="2019******005",select = c(3,4))
  if (nrow(my_data)) {
    all_data <- cbind(my_data,substring(f,3,5))
    results <- rbind(results,all_data)
  }

# extract hours minutes and seconds
times <- strsplit(as.character(results$总时长),"[时分秒]")
# transpose
times <- t(sapply(times,'[',1:3))
results <- cbind(results,times)
names(results) <- c("click_times","times","class","hour","minute","second")

I have saved the data in the .RData file (you can click the link to download it), because the local files are not accessible when Netlify build and host the website.

load("../../static/post/course_time.RData")
library(DT)
datatable(results,rownames = FALSE,
          colnames = c('Click', 'Times_all', 'Class', 'H', 'M','S'),
          caption = htmltools::tags$caption(
            style = 'caption-side: top; text-align: center;',
            htmltools::strong('Watching times of the classes')# bold text
          ))