[英]R programming using the cut() function to split a variable into 3 classes
I a have a dataframe called wine
with variables among which is the variable Spice
of integer type. 我有一个名为
wine
的数据帧,具有变量,其中是整数类型的变量Spice
。 I would like to split this variable ( Spice
) using the cut()
function into 3 classes ( <2; between 2 and 2.5; >3 ). 我想使用
cut()
函数将此变量( Spice
)分为3个类(<2; 2和2.5之间;> 3)。
Assuming you mean spice is numeric (and not integer, which would only fall between 2 and 2.5 if it was exactly two), it might be hard to do with just cut if you're trying to be left and right inclusive. 假设您的意思是香料是数字的(而不是整数,如果恰好是2,则不会在2到2.5之间),如果要左右兼顾,可能很难用剪切来实现。
You can get close with something like 您可以通过以下方式接近
dat <- data.frame(spice=5*runif(100))
dat$lvl <- cut(dat$spice, breaks=c(0,2,2.5,1e6), right=FALSE)
dat$lvl <- as.factor(as.numeric(dat$lvl))
though if the value for spice is exactly 2.5 it will be placed in group 3 instead of group 2. 尽管如果spice的值正好是2.5,它将被放置在组3中而不是组2中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.