嵌套ifelse R中出现意外的“(”

老和尚

我正在尝试在R中运行以下代码行

new <- data.frame(newdb, 
                ifelse(newdb$centile >=0 & newdb$centile < 3,"1",
                ifelse(newdb$centile >=3 & newdb$centile < 6,"2",
                       ifelse(newdb$centile >=6 & newdb$centile < 9,"3","4"))))

但是我收到以下错误

错误:“ ifelse(newdb $ centile> = 6&newdb $ centile <> 9,“ 3”,“ 4”))中出现意外的')'

阿克伦

我们不需要嵌套的ifelsecut通过指定,可以更轻松地解决该问题breaks

res1 <- cut(newdb$centile, breaks=c(-Inf, 0, 3, 6, 9, Inf),
                  right=FALSE, labels=FALSE)-1

OP的代码与 ifelse

res <- ifelse(newdb$centile >=0 & newdb$centile < 3,"1", 
          ifelse(newdb$centile >=3 & newdb$centile < 6,"2", 
            ifelse(newdb$centile >=6 & newdb$centile < 9,"3","4")))  

identical(as.numeric(res), res1)
#[1] TRUE

如果我们要在原始数据集中创建一个新列(“组”),

 newdb$group <- res

同样,newdb$可以通过使用OP避免代码中的倍数with

  with(newdb,
          ifelse(centile >=0 & centile < 3, '1',
            ifelse(centile >=3 & centile < 6, '2',
              ifelse(centile >=6 & centile < 9, '3', '4'))))

数据

set.seed(24)
newdb <- data.frame(centile= sample(5:12, 25, replace=TRUE))

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

R:由`ifelse()`嵌套

来自分类Dev

[...]中出现意外的“”

来自分类Dev

加快嵌套ifelse语句-R

来自分类Dev

R中嵌套ifelse语句的替代方法

来自分类Dev

嵌套ifelse函数是否有R函数?

来自分类Dev

R 错误:“else”中出现意外的“else”

来自分类Dev

PHP If嵌套HTML语句失败,出现意外的$ end错误

来自分类Dev

使用嵌套 for/foreach 循环时出现意外的文件结尾

来自分类Dev

Python中出现意外的“除外”加薪

来自分类Dev

输出中出现意外的断行

来自分类Dev

移位操作中出现意外结果

来自分类Dev

Powershell中出现意外的令牌错误

来自分类Dev

@import“程序中出现意外的'@'”

来自分类Dev

批量中出现意外的括号')'

来自分类Dev

laravel查询中出现意外结果

来自分类Dev

.bashrc别名中出现意外的EOF

来自分类Dev

移位操作中出现意外结果

来自分类Dev

输出中出现意外的断行

来自分类Dev

批量中出现意外的括号')'

来自分类Dev

代码中出现意外的令牌错误

来自分类Dev

R中嵌套ifelse语句中的值丢失

来自分类Dev

R:改进嵌套的ifelse语句和多种模式

来自分类Dev

在R中的嵌套数据框中多次调用ifelse

来自分类Dev

在R中匹配'+'时在pmatch中出现意外行为

来自分类Dev

简单R函数的语法错误(错误:“}”中出现意外的“}”)

来自分类Dev

在R中匹配'+'时在pmatch中出现意外行为

来自分类Dev

错误:删除 R 中包含 0 的列时,“}”中出现意外的“}”

来自分类Dev

SyntaxError:node.js中出现意外的令牌const {

来自分类Dev

使用graphicsMagick的collectionFS中出现意外的空writestream