我在使用colClasses
函数时遇到问题read.xlsx
我有以下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 ...
我想导入Treatment
为factor
. 为此,我尝试将该colClasses
函数用作参数,如下所示:
mydata1 <- read.xlsx("dataset_1.xlsx", sheetName = "dataset_1", colClasses = c("Treatment" = "factor", "Nitrate_conc" = "numeric"))
但是我收到以下错误:
错误
class(aux) <- colClasses[ic]
:将类添加factor
到无效对象
谁能指出我做错了什么?