Skip to content

Instantly share code, notes, and snippets.

@lincank
Last active December 14, 2015 15:09
Show Gist options
  • Save lincank/5106014 to your computer and use it in GitHub Desktop.
Save lincank/5106014 to your computer and use it in GitHub Desktop.
extract values from xls files
# JDK required, install jdk on mac with:
# R CMD javareconf JAVA_CPPFLAGS=-I/System/Library/Frameworks/JavaVM.framework/Headers
require(xlsx)
#file <- system.file("/tmp/", "cobbdoug.xlsx", package = "xlsx")
wb <- loadWorkbook("/tmp/cobbdoug.xlsx")
sheets <- getSheets(wb) # get all sheets
sheet <- sheets[['cobbdoug.xls']] # get sheet with name "cobbdoug.xls"
rows <- getRows(sheet)
cells <- getCells(rows, colIndex=2)
# apply getCellValue() function to all cells, return a list
values <- lapply(cells, getCellValue)
len = length(values)
# extract list values to a vector, without names
Q <- unlist(values, use.names = FALSE)
# convert characters in subvector to number,
Q <- as.numeric(Q[2:len])
cells <- getCells(rows, colIndex=3)
values <- lapply(cells, getCellValue)
K <- unlist(values, use.names = FALSE)
K <- as.numeric(K[2:len])
cells <- getCells(rows, colIndex=4)
values <- lapply(cells, getCellValue)
L <- unlist(values, use.names = FALSE)
L <- as.numeric(L[2:len])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment