arcee123 arcee123 - 2 months ago 16
R Question

how to read two columns in XLSX into R

I am trying to read an XLSX sheet into an R dataframe:

Dutch_List <- read.xlsx2("file.xlsx", sheetIndex = 1, startRow = 1, colIndex=5,endRow = 10000, as.data.frame = TRUE, header=TRUE)
Dutch_Status <- read.xlsx2("File.xlsx", sheetIndex = 1, startRow = 1, colIndex=8,endRow = 10000, as.data.frame = TRUE, header=TRUE)


Is there a way to read columns 5 and 8, without reading columns 5 through 8?
I just want the two columns...

Thanks

Answer

Use colIndex = c(5,8).

Also, as.data.frame is TRUE by default, so you don't need to include that argument if you want to save some typing. Likewise, if you want to read all rows, there's no need to include startRow and endRow.