Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read only certain columns from xls

Tags:

r

xls

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`?))

like image 630
moadeep Avatar asked Nov 27 '22 21:11

moadeep


2 Answers

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)
like image 83
Didzis Elferts Avatar answered Dec 06 '22 19:12

Didzis Elferts


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"))
like image 34
R. Prost Avatar answered Dec 06 '22 19:12

R. Prost