从现有变量生成新的二项式变量 [英] Generating a new binomial variable from existing variables

查看:62
本文介绍了从现有变量生成新的二项式变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下数据:

Var1 = (1,1,0,1,0,1,0,1,1,0,1,1,0,0,0,1,0)
Var2 = (1,0,0,1,1,0,0,1,0,1,0,1,1,1,0,1,1)
Var3 = (0,0,0,1,1,1,0,0,1,0,1,0,0,0,1,0,0)

使用 if / <$ R中的c $ c> else 语法,我需要创建新的 Var4 ,以便

Using if/else syntax in R, I need to create new Var4, so that

if var1=1 & var2=1 & var3=1 then var4=1
if var1=0 & var2=0 & var3=0 then var4=0
if var1=1 & var2=0 & var3=0 the var4=1 and so on.

基本上,当全部三个时, var4 = 0

Basically, var4=0 when all three variables=0 only.

推荐答案

我们可以 cbind 'var'向量,获取 rowSums 并检查其是否大于0,使用 +

We can cbind the 'var' vectors, get the rowSums and check if it is greater than 0, coerce the logical to binary with +

+(rowSums(cbind(Var1, Var2, Var3)) > 0)






或使用 |

as.integer(Var1|Var2|Var3)

这篇关于从现有变量生成新的二项式变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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