简体   繁体   English

使用 ifelse 在 r 中创建新变量并更改因变量

[英]Creating a new variable and altering dependent variables in r using ifelse

Let's say we have a df as follows:假设我们有一个 df 如下:

A  B  C  D  E
1  1  0  0  1
0  0  1  0  0
0  0  0  0  1
1  1  1  1  0
0  1  1  0  1
1  0  1  0  0   

So I would like to make another variable F which says, if the sum of A:D is greater than 1, F is 1 and A:D are 0 .所以我想创建另一个变量F ,它表示,如果 A:D 的总和大于 1,则 F 为 1 且 A:D 为 0

Additionally, If E == 1 , then F = 0 .此外,如果E == 1 ,则F = 0

So here's how I wrote it but it's not working...所以这就是我写它的方式,但它不起作用......

#Counter
df<- df %>% 
       mutate(case_count = A+B+C+D)

df$F <- ifelse(df$E == 1, 0,
              ifelse(df$case_count > 1, 
                     df$A == 0 & 
                     df$B == 0 &
                     df$C == 0 &
                     df$D == 0 &
                     df$F == 1, 0))

And the correct result here should then be那么这里的正确结果应该是

A  B  C  D  E  case_count F 
1  1  0  0  1           2 0
0  0  1  0  0           1 0
0  0  0  0  1           0 0
0  0  0  0  0           4 1
0  1  1  0  1           2 0
0  0  0  0  0           2 1

Using dplyr and the new functions across and c_across使用dplyr和新功能acrossc_across

df %>% 
  rowwise() %>% 
  mutate(
    case_count = sum(c_across(A:D)),
    F_ = ifelse(E == 1, 0, ifelse(case_count > 1, 1, 0))
    ) %>% 
  mutate(across(A:D, ~ifelse(F_ == 1, 0, .)))

I named the new column F_ instead of just F because the latter may be confused with the abbreviation for FALSE .我将新列命名为F_而不仅仅是F ,因为后者可能与FALSE的缩写混淆。

Output Output

# A tibble: 6 x 7
# Rowwise: 
#       A     B     C     D     E case_count    F_
#   <dbl> <dbl> <dbl> <dbl> <int>      <int> <dbl>
# 1     1     1     0     0     1          2     0
# 2     0     0     1     0     0          1     0
# 3     0     0     0     0     1          0     0
# 4     0     0     0     0     0          4     1
# 5     0     1     1     0     1          2     0
# 6     0     0     0     0     0          2     1

You can try this solution (DF is your original data):您可以尝试此解决方案(DF 是您的原始数据):

#Create index
DF$I1 <- rowSums(DF[,1:4])
DF[DF[,6]>1,1:4]<-0
#Create F
DF$F <- ifelse(DF$I1>1,1,0)
DF$F <- ifelse(DF$E==1,0,DF$F)

  A B C D E I1 F
1 0 0 0 0 1  2 0
2 0 0 1 0 0  1 0
3 0 0 0 0 1  0 0
4 0 0 0 0 0  4 1
5 0 0 0 0 1  2 0
6 0 0 0 0 0  2 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM