选择具有 + 和 - z 分数值的组

问题描述 投票:0回答:1

我希望按列

type
对数据进行分组,并选择(分组的
type
中)同时具有正和负 z 分数的所有行。换句话说,我希望仅当 z 分数范围高于或低于 0 时才选择
type
分组。

在示例数据

test
中,我希望将
type
的“翻译”、“信号转导”、“聚糖生物合成”和“遗传信息处理”分开。我真的很纠结从哪里开始,下面是我最好的起点。

out <- test %>% group_by(type) %>% select(zscore < 0 & zscore >0)

> dput(test)
structure(list(zscore = c(0.775767217942226, 1.50431110062286, 
0.96844973768628, 1.13270780763826, -0.417688838401554, -1.16388703634018, 
-0.79027804814387, 0.497003183210411, -0.79027804814387, -1.16388703634018, 
1.37434643916904, -0.79027804814387, -0.79027804814387, -1.16388703634018, 
-0.572244270676755, -1.16388703634018, 0.291606810323695, 0.44589189244246, 
1.1176046488225, -1.16388703634018, 0.21485327846621, -0.417688838401554, 
-1.16388703634018, -0.117261781255063, -1.16388703634018, 1.22796644918627, 
-0.417688838401554, -1.16388703634018, -0.19999370182559, -1.16388703634018
), type = c("Nucleotide Metabolism", "Folding Sorting and Degradation", 
"Cell Motility", "Genetic Information Processing", "Carbohydrate Metabolism", 
"Glycan Biosynthesis and Metabolism", "Carbohydrate Metabolism", 
"Genetic Information Processing", "Metabolism", "Carbohydrate Metabolism", 
"Membrane Transport", "Transport and Catabolism", "Genetic Information Processing", 
"Amino Acid Metabolism", "Transport and Catabolism", "Translation", 
"Translation", "Lipid Metabolism", "Glycan Biosynthesis and Metabolism", 
"Amino Acid Metabolism", "Genetic Information Processing", "Glycan Biosynthesis and Metabolism", 
"Carbohydrate Metabolism", "Metabolism", "Xenobiotics Biodegradation and Metabolism", 
"Signal Transduction", "Genetic Information Processing", "Signal Transduction", 
"Carbohydrate Metabolism", "Metabolism")), row.names = c(1L, 
2L, 3L, 4L, 6L, 7L, 9L, 15L, 17L, 20L, 23L, 24L, 27L, 28L, 32L, 
35L, 36L, 37L, 38L, 39L, 41L, 44L, 45L, 47L, 48L, 49L, 51L, 52L, 
54L, 57L), class = "data.frame")
r dataframe subset
1个回答
2
投票

您应该使用

filter()
而不是
select()
来选择行。还可以使用
any()
查看组中的所有值,因为任何单个值不能同时为负值和正值。尝试一下

test %>% 
  group_by(type) %>%
  filter(any(zscore < 0) & any(zscore >0))
© www.soinside.com 2019 - 2024. All rights reserved.