Does anyone know why argument colClasses
does not seem to work in read.xlsx
?
I create a sample *.xlsx file:
> library(xlsx)
> d1 = data.frame(A=LETTERS[1:3], B=letters[1:3], C=1:3, D=c(1.1, NA, NA))
> str(d1)
'data.frame': 3 obs. of 4 variables:
$ A: Factor w/ 3 levels "A","B","C": 1 2 3
$ B: Factor w/ 3 levels "a","b","c": 1 2 3
$ C: int 1 2 3
$ D: num 1.1 NA NA
> write.xlsx(d1, 'test.xlsx', sheetName='Sheet1', row.names=F, showNA=F)
then try to read it with read.xlsx
, without and with colClasses
argument:
> d2 = read.xlsx('test.xlsx', sheetName='Sheet1')
> str(d2)
'data.frame': 3 obs. of 4 variables:
$ A: Factor w/ 3 levels "A","B","C": 1 2 3
$ B: Factor w/ 3 levels "a","b","c": 1 2 3
$ C: num 1 2 3
$ D: num 1.1 NA NA
> d2 = read.xlsx('test.xlsx', sheetName='Sheet1', colClasses=c(B='character', 'A'='character'))
> str(d2)
'data.frame': 3 obs. of 4 variables:
$ A: Factor w/ 3 levels "A","B","C": 1 2 3
$ B: Factor w/ 3 levels "a","b","c": 1 2 3
$ C: num 1 2 3
$ D: num 1.1 NA NA
The problem is colClasses
seems to have no effect. Any ideas?
Thank you for your help.
Aleksey
P.S. I have R 3.0.1, xlsx
0.5.1
stringsAsFactors
does the second pass conversion, casting character vectors as factors. Thank you, and sorry for forgetting about this question for so long. – Jinn