使用 cut() 函数时意外的 ')' 错误

Error of unexpected ')' when using cut() function

所以我正在尝试使用 cut 函数将数字变量转换为分类变量,但我一直收到意外字符错误,但我已经对拼写进行了三重检查,所以不确定哪里出了问题,任何人都可以帮助?这是代码:

[ca_timechcare_categorical:= 切(ca_timechcare, break=c(0,36,72,108,Inf),

include.lowest=TRUE,

labels=c("Very_Low", "Low", "High", "Very_High"))]

dataset[,table(ca_timechcare_categorical)]

这是我收到的错误:

Error: unexpected ')' in "labels=c("Very_Low", "Low", "High", "Very_High"))"

谢谢!

更新:现在已按照以下建议将代码更正为:

dataset <- as.data.table(US_COVID19_study)

dataset[,ca_timechcare_categorical:= cut(survey_design_childcare,
                                                  breaks = c(36,72,108,Inf),
                                                  labels= c("Very_Low", "Low", "High", "Very_High"))]

dataset[,table(ca_timechcare_categorical)]

但现在我得到这个错误:

Error in `:=`(ca_timechcare_categorical, cut(survey_design_childcare,  : 
  Check that is.data.table(DT) == TRUE. Otherwise, := and `:=`(...) are defined for use in j, once only and in particular ways. See help(":=").

有什么想法吗??再次感谢!

我假设您使用的是 data.table,因此是残留的操作数 := 两件事:你在括号中缺少一个逗号,它的 breaks 不是 break

library(data.table)
irisdt <- as.data.table(iris)

irisdt[,new := cut(Sepal.Width,
                   breaks=c(0,36,72,108,Inf),
                   include.lowest=TRUE,
                   labels=c("Very_Low", "Low", "High", "Very_High"))]