嵌套的if-else语句,仅测试第一个条件

时间:2020-01-28 00:45:56

标签: r if-statement dplyr statistics

我有一个数据集“ cipe”,并且如果以下任何条件“ htn”,“ sle”,“ apls”,“糖尿病”,“ ckd”为遇见

我使用的代码是:

cipe <- mutate(cipe, case == ifelse(htn == 1, "1",
                                ifelse(sle == 1, "1",
                                       ifelse(apls == 1, "1",
                                              ifelse(diabetes == 1, "1",
                                                     ifelse(ckd == 1, "1", "0"))))))

我的数据集具有约840k的观测值,因此发布是不可行的,但是当我运行此代码时,新的“ case”变量中的“ 1”数量恰好等于“”中的“ 1”数量htn”,(应该更高),因此未评估其他测试。任何帮助都会很棒。谢谢

1 个答案:

答案 0 :(得分:0)

我们可以对数据集“ cipe”的列进行子集设置,通过与1进行比较来创建逻辑矩阵,使用df["widths"] = df["max_score"]-df["min_score"] ax2 = ax.twiny() ax2.barh(df.a, df.widths, left=df.min_score, color="grey") plt.xlim([0,100]) ax.legend(loc='lower left') 检查其是否大于0,使用rowSums将逻辑转换为二进制或+

as.integer

或者另一个选择是+(rowSums(cipe[c('sle', 'apls', 'diabetes', 'ckd')] == 1) > 0) Reduce

|