Read only certain columns from xls

2020-03-26 05:45发布

I have an excel spreadsheet with several sheets. The format is as below:

Date        A       B       C       D       E       F                       Reference   Ref Date    Half life
03/01/13    6.29    5.28    8.15    4.93    11.67   6.4                     8.88        01/01/99    30.23
04/01/13    6.39    5.39    8.22    5.04    11.75   6.4                 
07/01/13    6.34    5.32    8.17    4.92    11.82   6.4                 
08/01/13    6.33    5.3 8.16    4.96    11.68   6.4                 
09/01/13    6.29    5.29    8.13    4.93    11.73   6.4                 
10/01/13    6.29    5.32    8.17    4.95    11.61   6.4                 
11/01/13    6.21    5.27    8.12    4.95    11.57   6.4                 
14/01/13    6.28    5.28    8.09    4.92    11.65   6.4                 
15/01/13    6.25    5.26    8.06    4.9 11.59   6.4                 
16/01/13    6.26    5.3 8.08    4.94    11.7    6.4                 
17/01/13    6.27    5.27    8.09    4.96    11.57   6.4                 
18/01/13    6.29    5.27    8.04    4.94    11.57   6.4                 
21/01/13    6.34    5.33    8.23    4.99    11.68   6.4                 
22/01/13    6.31    5.34    8.23    4.97    11.63   6.4                 
23/01/13    6.24    5.26    8.03    4.93    11.58   6.4                 
24/01/13    6.24    5.27    8.03    4.93    11.56   6.4                 
25/01/13    6.26    5.3 8.08    4.93    11.6    6.4 

I want to read the colmuns Date and A-F only into a data frame and ignore the elements in J1,2,K1,2 and L,1,2 which are used to calculate some values.

I am using read.xls from the package gdata

read.xls("filename.xls", "sheetname", na.strings=c("NA","#DIV/0!"))

However this gives me a data frame containing columns A - L. I could recast this data frame to the desired format but would prefer to choose which columns to read. Can I do this with read.xls in a similar manner that read.table can achieve this (Ways to read only select columns from a file into R? (A happy medium between `read.table` and `scan`?))

标签: r xls
3条回答
你好瞎i
2楼-- · 2020-03-26 06:37

You can use library XLConnect to read .xls files. Function readWorksheet() lets you set columns and rows you need to import.

library(XLConnect)
wb<-loadWorkbook("wb.xls")
data <- readWorksheet(wb, sheet = "Sheet1",startCol=1,endCol=7)
查看更多
三岁会撩人
3楼-- · 2020-03-26 06:44

Another option is to use is to use Excel named range with RODBC.

channel<-odbcConnectExcel(paste(Repository,excelFile, sep = "/"))
ConsoTab<-sqlFetch(channel,NamesRangeAF)
odbcClose(channel)

Here NamesRangeAF is your Excel named Range.

查看更多
三岁会撩人
4楼-- · 2020-03-26 06:45

With the readxl package using read_xlsx you can specify the range. Use read_xls or read_excel depending on file type.

library(readxl)
df <- read_xlsx(path = "filename.xlsx", sheet = "sheetname", range = cell_cols("A:G"))
查看更多
登录 后发表回答