麻烦大家帮忙看看分组结果不正确,这个函数哪里出问题,谢谢
提示: In if (x <= 1) { ... :
the condition has length > 1 and only the first element will be used
代码如下:
library(data.table)
fun<-function(x){
if(x<=1){
y="<1"
}
else if(1<x & x<=20){
y="1~20"
}
else if(20<x & x<=40){
y="20~40"
}
else if(40<x & x<=60){
y="40~60"
}
else if(60<x & x<=80){
y="60~80"
}
else if(80<x & x<=100){
y="80~100"
}
else if(100<x & x<=120){
y="100~120"
}
else if(120<x & x<=150){
y="120~150"
}
else if(150<x & x<=200){
y="150~200"
}
else if(200<x & x<=300){
y="200~300"
}
else
y=">300"
return(y)
}
set.seed(112)
dt<-data.table(name=sample(LETTERS,50,TRUE),score=rep(1:50))
dt[,list(score=sum(score),分组=fun(score)),by="name"]


雷达卡






京公网安备 11010802022788号







