r - 从数据框中子集整个集群
问题描述
在下面的 data.frame 中,我想知道如何对一个比其中更大的整个集群进行子study
集outcome
化1
?
我想要的输出如下所示。我试过subset(h, outcome > 1)
了,但这并没有给出我想要的输出。
h = "
study outcome
a 1
a 2
a 1
b 1
b 1
c 3
c 3"
h = read.table(text = h,h=T)
DESIRED OUTPUT:
"
study outcome
a 1
a 2
a 1
c 3
c 3"
解决方案
修改subset
-
- 基于第一个逻辑表达式的“研究”子集
outcome > 1
%in%
在“研究”上使用以创建最终的逻辑表达式subset
subset(h, study %in% study[outcome > 1])
-输出
study outcome
1 a 1
2 a 2
3 a 1
6 c 3
7 c 3
如果我们想限制'结果'值为1的'study'元素的数量,即第一个'n''study',然后unique
从子集的第一个表达式中获取'study',head
用于获取第一个'n' “研究”值并用于%in%
创建逻辑表达式
n <- 3
subset(h, study %in% head(unique(study[outcome > 1]), n))
或者可以通过一组方法来完成any
library(dplyr)
h %>%
group_by(study) %>%
filter(any(outcome > 1)) %>%
ungroup
推荐阅读
- visual-studio - 无法创建 IOS 存档:路径(目录名必须少于 248 个字符)和/或文件名(必须少于 260 个字符)太长
- wordpress - WordPress:如何获取“init”钩子的完整路径?或者它是另一个更好的钩子?
- python - Pytorch 错误:优化器在 Linux 服务器上得到了一个空参数列表
- angular - Angular slickgrid 自定义日期过滤器不适用于日期范围
- c# - 即使在 C# 的显式构造函数中未指定默认值,如何初始化
- json - 如何从 AMFPHP 库中获取 Json 响应?
- python - 如何计算两个相似文件的相同哈希?
- python - Django ModelForm 保存到另一个数据库
- laravel - Laravel:自定义数组验证取决于先前选择的值
- ssl-certificate - 使用 ddev 环境中的主机证书连接远程系统