如何根据 R 中的条件创建新变量 [英] How can I create a new variable based on conditions in R
问题描述
我正在尝试根据某些条件创建一个新变量.
I am trying to create a new variable based on some conditions.
我的数据看起来像
a b
1 NA
2 3
3 3
NA 2
NA NA
我想要的是一个变量 c
使得
What I want is a variable c
such that
当
a
不是NA
时,b
是NA
,c = a
当a
是NA
时,b
不是NA
,c = b
当a
为NA
时,b
为NA
,c = NA
当 a
不是 NA
时,b
不是 NA
,并且 a== b
, c = a
when a
is not NA
, b
is not NA
, and a == b
, c = a
当 a
不是 NA
时,b
不是 NA
,并且 a!= b
, c = "multiple_values"
when a
is not NA
, b
is not NA
, and a != b
, c = "multiple_values"
我该怎么做?
似乎 ifelse()
不能做我想做的事.
It seems like ifelse()
can't do what I want.
推荐答案
除了条件之一,即'a'、'b'中的非NA元素,且彼此不相等,所有其他coalesce
满足条件.因此,我们可以通过应用 coalesce
Except for one of the condition, i.e non-NA elements in both 'a', 'b', and they are not equal to each others, all other conditions are met with coalesce
. So, we can do a case_when
to generate the "multiple_values" based on the last condition and all others by applying coalesce
library(dplyr)
df1 %>%
mutate(c = case_when(!is.na(a) & !is.na(b) & a != b ~ "multiple_values",
TRUE ~ as.character(coalesce(a, b))))
# a b c
#1 1 NA 1
#2 2 3 multiple_values
#3 3 3 3
#4 NA 2 2
#5 NA NA <NA>
数据
df1 <- structure(list(a = c(1L, 2L, 3L, NA, NA), b = c(NA, 3L, 3L, 2L,
NA)), class = "data.frame", row.names = c(NA, -5L))
这篇关于如何根据 R 中的条件创建新变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!