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

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

问题描述

我想要一种简单的方法来创建一个新变量,以确定布尔值在 R 数据帧中是否为真.这是和示例:假设在数据集中我有 2 个变量(以及其他不相关的变量)a"和b",a"确定一个组,而b"是一个布尔值,值为 TRUE (1) 或 FALSE (0).我想创建一个变量c",对于b"至少一次为真"的组中的所有条目,它也是一个布尔值,为 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 中做过类似的事情,而且在互联网上很难找到这方面的信息.事实上,我这样做只是为了以后删除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 选项是

A base R option would be

 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天全站免登陆