用R dyplyr有条件地重新编码/替换变量? [英] Recode/replace variables conditionally with R dyplyr?

查看:72
本文介绍了用R dyplyr有条件地重新编码/替换变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码有条件地重新编码我的变量,以便在 var1 中存在大于 0 的任何值时,将 var 2 中的值替换为 NA.如果 var1 没有大于 0 的值(编码为 NA),则 var2 中的值应保持原样.下面的代码行将 var2 中的所有内容编码为 NA,如果 var1 为 NA,则不保留 var2 中的值.我也尝试使用 na_if() 和 coalesce() ,但没有太大成功.有没有可能解决这个问题?

I am trying to use the following code to recode my variables conditionally so that the values in var 2 are replaced by NA if there is any value greater than 0 in var1. If var1 has no value greater than 0 (which is coded as NA), then the value in var2 should remain as it is. The line of code below codes everything in var2 to NA and does not keep the values in var2 if var1 is NA. I have also tried to use na_if() and coalesce() with not much success. Any possible fix to this?

df <-  df %>% 
    mutate(var2 = if_else(var1 > 0, NA, var2 = TRUE ))

推荐答案

我们不需要 var2 = TRUE,而应该是 var2

We don't need var2 = TRUE, instead it should be var2

library(dplyr)
df %>%
    mutate(var2 = ifelse(var1 > 0, NA, var2))

if_else 是特定于类型的,因此您需要具有与 'var2' 类型匹配的正确 NA 类型.假设它是 numeric :

if_else is type specific, so you need to have the correct NA type matching the 'var2' type. Assuming it is numeric :

df %>%
   mutate(var2 = if_else(var1 > 0, NA_real_, var2))

<小时>

但是,这也可以通过:


But, this can be also done with :

df %>%
    mutate(var2 = replace(var2, var1 > 0, NA))

或者使用 case_when :

df %>%
     mutate(var2 = case_when(var1 > 0 ~ NA_real_, TRUE ~ var2))

这篇关于用R dyplyr有条件地重新编码/替换变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆