r中的if条件 [英] multiple if conditions in r

查看:74
本文介绍了r中的if条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究此问题,但未能解决.我知道答案可能很简单,但我无法解决,也无法从其他类似问题中找到解决方法.

I have been working on this problem, but failed to solve it. I know the answer might be very easy, but I could not solve it and could not find how to solve it from other similar questions as well.

我有以下data.frame:

I have got the following data.frame:

region    group   probs1   probs2   probs3   probs4     weights
   1        2       0.2       0.3     0.4       0.1        NA
   2        4       0.3       0.4     0.15      0.15       NA
   3        3       0.4       0.1     0.3       0.2        NA
   4        1       0.7       0.1     0.1       0.1        NA
   5        1       0.2       0.3     0.4       0.1        NA
   6        2       0.6       0.1     0.1       0.2        NA
   7        3       0.7       0.1     0.1       0.1        NA
   8        4       0.3       0.2     0.1       0.4        NA
   9        3       0.2       0.1     0.1       0.6        NA
  10        1       0.1       0.2     0.1       0.6        NA

我要做的是在data.frame中创建一个称为权重"的新列,该列的计算方式类似于group == 1,然后weights = probs1/probs1.如果group == 2,则权重= probs1/probs2.如果group == 3,则权重= probs1/probs3.如果group == 4,则权重= probs1/probs4.

What I am going to do is to create a new column in the data.frame called "weights" that is calculated as if group==1, then weights=probs1/probs1. If group==2, then the weights=probs1/probs2. If group==3, then the weights=probs1/probs3. If group==4, then the weights=probs1/probs4.

我使用了不同类型的代码,例如 ifelse if .... else dplyr ,但是我失败了.实际上,可能的代码只会为weights = probs1/probs1创建权重,并将其应用于所有区域,与组无关.

I used different types of codes like, ifelse, if....else, dplyr, but I failed. In fact, may codes only create the weights for weights=probs1/probs1 and apply it for all the regions, regardless of the group.

有人可以帮我解决吗?谢谢

May someone please help me to solve it? Thanks

推荐答案

您可以使用 dplyr :: case_when

library(dplyr)
df %>%
    mutate(weights = case_when(
        group == 1 ~ probs1 / probs1,
        group == 2 ~ probs1 / probs2,
        group == 3 ~ probs1 / probs3,
        TRUE ~ probs1 / probs4))
#   region group probs1 probs2 probs3 probs4   weights
#1       1     2    0.2    0.3   0.40   0.10 0.6666667
#2       2     4    0.3    0.4   0.15   0.15 2.0000000
#3       3     3    0.4    0.1   0.30   0.20 1.3333333
#4       4     1    0.7    0.1   0.10   0.10 1.0000000
#5       5     1    0.2    0.3   0.40   0.10 1.0000000
#6       6     2    0.6    0.1   0.10   0.20 6.0000000
#7       7     3    0.7    0.1   0.10   0.10 7.0000000
#8       8     4    0.3    0.2   0.10   0.40 0.7500000
#9       9     3    0.2    0.1   0.10   0.60 2.0000000
#10     10     1    0.1    0.2   0.10   0.60 1.0000000


样本数据

df <- read.table(text =
    "region    group   probs1   probs2   probs3   probs4     weights
   1        2       0.2       0.3     0.4       0.1        NA
   2        4       0.3       0.4     0.15      0.15       NA
   3        3       0.4       0.1     0.3       0.2        NA
   4        1       0.7       0.1     0.1       0.1        NA
   5        1       0.2       0.3     0.4       0.1        NA
   6        2       0.6       0.1     0.1       0.2        NA
   7        3       0.7       0.1     0.1       0.1        NA
   8        4       0.3       0.2     0.1       0.4        NA
   9        3       0.2       0.1     0.1       0.6        NA
  10        1       0.1       0.2     0.1       0.6        NA", header = T)

这篇关于r中的if条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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