gpt4 book ai didi

基于3列组合并排除重复组合的R子集数据框

转载 作者:行者123 更新时间:2023-12-04 12:32:21 26 4
gpt4 key购买 nike

我有一个看起来像这样的数据集:

Experiment  Sequence    Parameter   Time
Exp1 AAAA 2 10
Exp2 AAAA 2 11
Exp3 AAAA 2 12
Exp1 BBBB 2 13.1
Exp1 BBBB 3 13.2
Exp1 BBBB 4 13.3
Exp2 BBBB 2 14.1
Exp2 BBBB 3 14.2
Exp3 BBBB 2 16.3
Exp3 BBBB 3 16.4
Exp3 BBBB 4 16.5
Exp3 BBBB 5 16.6
Exp1 CCCC 2 20
Exp2 CCCC 2 22.2
Exp1 DDDD 3 22.3
Exp1 DDDD 2 22.4
Exp2 DDDD 3 25.2
Exp2 DDDD 2 25.3
Exp3 DDDD 3 27
Exp1 EEEE 2 28
Exp2 EEEE 3 29
Exp3 EEEE 4 30
Exp1 FFFF 2 33.2
Exp1 FFFF 3 33.4
Exp1 FFFF 4 33.6
Exp2 FFFF 2 35.1
Exp2 FFFF 3 35.2
Exp1 GGGG 2 40.1
Exp1 GGGG 2 40.2
Exp1 GGGG 2 40.3
Exp1 GGGG 2 42
Exp2 GGGG 2 42.3
Exp2 GGGG 2 44.3
Exp3 GGGG 2 45.3
Exp3 GGGG 2 45.4

真实数据集有:

  • 许多实验
  • 许多序列,可以出现在一个或多个实验中
  • 每个Sequence可以有3-4个不同的参数

对于我的分析(当时),我首先需要根据前 3 列的组合对数据帧进行子集化:实验、序列和参数。规则是:

  • 序列+参数组合应该存在于数据框的所有实验中。如果不是,则应将其排除在外。在示例中:BBBB+4, BBBB+5, CCCC+2, ... 应该离开,BBBB+2, DDDD+3,... 可以留下
  • 如果序列+参数组合在同一实验中多次出现,则也应排除这些组合。在示例中:GGGG+2

所以例子中的dataframe经过子集化后应该变成这样:

Experiment  Sequence    Parameter   Time
Exp1 AAAA 2 10
Exp2 AAAA 2 11
Exp3 AAAA 2 12

Exp1 BBBB 2 13.1
Exp2 BBBB 2 14.1
Exp3 BBBB 2 16.3

Exp1 BBBB 3 13.2
Exp2 BBBB 3 14.2
Exp3 BBBB 3 16.4

Exp1 DDDD 3 22.3
Exp2 DDDD 3 25.2
Exp3 DDDD 3 27

有人可以帮助我吗?谢谢!

Experiment <- c("Exp1", "Exp2", "Exp3", "Exp1", "Exp1", "Exp1", "Exp2", "Exp2", "Exp3", "Exp3", "Exp3", "Exp3", "Exp1", "Exp2", "Exp1", "Exp1", "Exp2", "Exp2", "Exp3", "Exp1", "Exp2", "Exp3", "Exp1", "Exp1", "Exp1", "Exp2", "Exp2", "Exp1", "Exp1", "Exp1",   "Exp1", "Exp2", "Exp2", "Exp3", "Exp3")
Sequence <- c("AAAA", "AAAA", "AAAA", "BBBB", "BBBB", "BBBB", "BBBB", "BBBB", "BBBB", "BBBB","BBBB", "BBBB", "CCCC", "CCCC", "DDDD", "DDDD", "DDDD", "DDDD", "DDDD", "EEEE", "EEEE", "EEEE", "FFFF", "FFFF", "FFFF", "FFFF", "FFFF", "GGGG", "GGGG", "GGGG", "GGGG", "GGGG", "GGGG", "GGGG", "GGGG")
Parameter <- c("2", "2", "2", "2", "3", "4", "2", "3", "2", "3", "4", "5", "2", "2", "3", "2", "3", "2", "3", "2", "3", "4", "2", "3", "4", "2", "3", "2", "2", "2", "2", "2", "2", "2", "2")
Time <- c(10.0, 11.0, 12.0, 13.1, 13.2, 13.3, 14.1, 14.2, 16.3, 16.4, 16.5, 16.6, 20.0, 22.2, 22.3, 22.4, 25.2, 25.3, 27.0, 28.0, 29.0, 30.0, 33.2, 33.4, 33.6, 35.1, 35.2, 40.1, 40.2, 40.3, 42.0, 42.3, 44.3, 45.3, 45.4)
df <- data.frame(Experiment, Sequence, Parameter, Time)

最佳答案

一个选项是data.table。我们将“data.frame”转换为“data.table”(setDT(df),按“Sequence”、“Parameter”、if 的频率分组>unique 'Experiment' 中的元素是 3,我们对 Data.table (.SD) 进行子集化,然后,我们按 'Experiment'、'Sequence' 和 'Parameter' 分组,如果nrow 等于 1 (.N==1) 我们子集 Data.table (.SD)。

library(data.table)
setDT(df)[, if(uniqueN(Experiment)==3) .SD, by = .(Sequence, Parameter)
][,if(.N ==1) .SD , by = .(Experiment,Sequence, Parameter)]
# Experiment Sequence Parameter Time
# 1: Exp1 AAAA 2 10.0
# 2: Exp2 AAAA 2 11.0
# 3: Exp3 AAAA 2 12.0
# 4: Exp1 BBBB 2 13.1
# 5: Exp2 BBBB 2 14.1
# 6: Exp3 BBBB 2 16.3
# 7: Exp1 BBBB 3 13.2
# 8: Exp2 BBBB 3 14.2
# 9: Exp3 BBBB 3 16.4
#10: Exp1 DDDD 3 22.3
#11: Exp2 DDDD 3 25.2
#12: Exp3 DDDD 3 27.0

关于基于3列组合并排除重复组合的R子集数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33646397/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com