R 4.4.0 中的 mutate 和 if 问题[重复]

问题描述 投票:0回答:1

我将 R 更新到了新版本 4.4.0,我发现了以前没有的错误

例如,这曾经有效:

temp <- data.frame("a"=c(1,2),"b"=c(3,4),"c"=0)

temp <- temp %>% mutate(c=if(a>b){100}else{200})

每行 c 列都会更新为 200。但现在我得到了这个错误 enter image description here

那么,根据条件进行更新的新正确方法是什么?

r if-statement mutate
1个回答
0
投票

if
始终需要并且一次需要一个值(它不是矢量化的),因此会出现错误消息。

要么做

library(dplyr)

temp %>% rowwise() %>% mutate(c=if(a>b){100}else{200})

temp %>% mutate(c=if_else(a>b, 100, 200))
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.