0

我正在使用最新版本的 R(我是 R 新手,上周刚拿到它,正在学习用它绘制图表)。

我的代码:

df$bins <- ifelse(df$ROW_A <= 0.1, "0-0.1",
 ifelse(df$ROW_A <= 0.5, "0.1-0.5",
 ifelse(df$ROW_A<=1, "0.5-1",
 ifelse(df$ROW_A <= 2, "1-2",
 ifelse(df$ROW_A <=5, "2-5",
 ifelse(df$ROW_A <=10, "5-10",
 ifelse(df$ROW_A >10, ">10","NA")))))))

我不断收到错误消息:

+ifelse(df$ROW_A > 10, ">10", "NA") 中的错误:一元运算符的参数无效

谁能告诉我出了什么问题?

4

1 回答 1

0

作为替代方案,您可以使用cut

df$bins <- cut(df$ROW_A, breaks = c(0, 0.1, 0.5, 1, 2, 5, 10, Inf), 
               labels = c("0-0.1", "0.1-0.5",  "0.5-1", "1-2", "2-5", "5-10", ">10"))
于 2020-12-21T14:34:26.707 回答