I am having a problem using the colClasses
function in read.xlsx
I have the following data.frame
mydata <- read.xlsx("dataset_1.xlsx", sheetName = "dataset_1")
head(mydata)
Treatment Nitrate_conc
1 1 12
2 1 12
3 1 15
4 1 16
5 1 12
6 2 18
str(mydata)
data.frame': 20 obs. of 2 variables:
$ Treatment : num 1 1 1 1 1 2 2 2 2 2 ...
$ Nitrate_conc: num 12 12 15 16 12 18 25 26 28 28 ...
I want to import Treatment
as a factor
. to do this I have attempted to use the colClasses
function as an argument as shown below:
mydata1 <- read.xlsx("dataset_1.xlsx", sheetName = "dataset_1", colClasses = c("Treatment" = "factor", "Nitrate_conc" = "numeric"))
However I get the following error:
Error in
class(aux) <- colClasses[ic]
: adding classfactor
to an invalid object
Can anyone point out what I am doing wrong?