简体   繁体   中英

Read only certain rows from xls with R

I know it is possible to read only certain columns with the package XLConnect. But is it also possible to only read certain rows?

demoExcelFile <- system.file("demoFiles/mtcars.xlsx", package = "XLConnect")  
wb <- loadWorkbook(demoExcelFile)
# Read the columns 1, 3 and 5 from the sheet 'mtcars' (with default header = TRUE)
data <- readWorksheet(wb, sheet = "mtcars", keep=c(1,3,5))
  1. Edit: For consecutive rows, there is a possibility. Is there a possiblity for non-consecutive rows? So far, my method is to delete the "other" rows which aren't needed (but that is a lot of typing).

read all rows using readWorksheet and then select certain rows from data.frame you've got

for example:

data <- readWorksheet(wb, sheet = "mtcars") 
new.data <- data[c(10, 20, 30), ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM