查找R中的组布尔值是否为真 [英] finding if boolean is ever true by groups in R

查看:123
本文介绍了查找R中的组布尔值是否为真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一种简单的方法来创建一个新变量,以确定R数据帧中的布尔值是否为真. 这是示例: 假设在数据集中我有2个变量(以及其他不相关的变量)"a"和"b",而"a"确定了一个组,而"b"是具有值TRUE(1)或FALSE(0)的布尔值.我想创建一个变量"c",对于"b"至少一次为"TRUE"的组中的所有条目,它也是一个布尔值,为1,对于"b"从不为真的组中的所有条目,其布尔值为0. 从下面的条目中:

I want a simple way to create a new variable determining whether a boolean is ever true in R data frame. Here is and example: Suppose in the dataset I have 2 variables (among other variables which are not relevant) 'a' and 'b' and 'a' determines a group, while 'b' is a boolean with values TRUE (1) or FALSE (0). I want to create a variable 'c', which is also a boolean being 1 for all entries in groups where 'b' is at least once 'TRUE', and 0 for all entries in groups in which 'b' is never TRUE. From entries like below:

a   b
-----
1   1 
2   0
1   0
1   0
1   1
2   0
2   0
3   0
3   1
3   0

我想获取变量"c",如下所示:

I want to get variable 'c' like below:

a   b   c
-----------
1   1   1 
2   0   0
1   0   1
1   0   1
1   1   1
2   0   0
2   0   0
3   0   1
3   1   1
3   0   1
-----------

我知道如何在Stata中做到这一点,但是我还没有在R中做过类似的事情,并且很难在Internet上找到关于它的信息. 实际上,我这样做只是为了以后删除所有'c'为0的观察值,因此任何其他建议也都可以.该方法的应用涉及多项式logit估计,其中需要在估计之前从数据集中删除从未选择的备选方案.

I know how to do it in Stata, but I haven't done similar things in R yet, and it is difficult to find information on that on the internet. In fact I am doing that only in order to later remove all the observations for which 'c' is 0, so any other suggestions would be fine as well. The application of that relates to multinomial logit estimation, where the alternatives that are never-chosen need to be removed from the dataset before estimation.

推荐答案

基本R选项为

 df1$c <- with(df1, ave(b, a, FUN=any))

 library(sqldf)
 sqldf('select * from df1
      left join(select a, b,
         (sum(b))>0 as c
         from df1 
         group by a)
         using(a)')

这篇关于查找R中的组布尔值是否为真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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