gpt4 book ai didi

r - 按行号将数据框拆分为数据框列表

转载 作者:行者123 更新时间:2023-12-04 08:41:36 25 4
gpt4 key购买 nike

我有一个具有可变数量或行的数据框,并希望将其按附近的行组拆分为数据框列表(或只是多个 dfs)。应该分组在一起的附近行可以通过变量列来理解,其中每个组由数字上彼此相邻的样本组成。例如:第 1-9 行是一组,第 10-12 行是第 2 组,等等。变量列中内容的确切身份将与黑白数据集不一致。
这是df:

> conseq_high
variable 6 7 8 9 10 11 12 13 14 position rnum
1 pep_14 L L L L Q L S C S 3 1
2 pep_15 L L L Q L S C S Y 4 2
3 pep_16 L L Q L S C S Y A 5 3
4 pep_17 L Q L S C S Y A G 6 4
5 pep_18 Q L S C S Y A G Q 7 5
6 pep_19 L S C S Y A G Q F 8 6
7 pep_20 S C S Y A G Q F R 9 7
8 pep_21 C S Y A G Q F R V 10 8
9 pep_22 S Y A G Q F R V I 11 9
10 pep_136 E L K V E D P F Y 19 10
11 pep_137 L K V E D P F Y W 20 11
12 pep_138 K V E D P F Y W V 21 12
13 pep_164 V S V G L V F L F 26 13
14 pep_165 S V G L V F L F L 27 14
15 pep_166 V G L V F L F L Q 28 15
16 pep_175 H R L R G K L R A 30 16
17 pep_176 R L R G K L R A E 31 17
18 pep_223 Y N W L H R R L A 36 18
19 pep_224 N W L H R R L A G 37 19
我尝试获取必须按以下方式分组的每组行中最后一行的位置:
#subset out individual groups of peptides
as_vector(conseq_high$position) -> seq2
xy1 <- c(diff(seq2),0)
which(xy1 !=1) -> grp_ids #these are the ending positions of each group of peptides

> grp_ids
[1] 9 12 15 17 19
从这里开始,我遇到了实际分割出我想要的行组的问题。我尝试使用拆分和拼接但没有成功。有什么建议?
提前致谢!

最佳答案

我们可以用 cumsum 创建一个分组索引基于 diff 创建的逻辑向量“位置”中的相邻元素不是连续的,即差异不等于 1

lst1 <-  split(conseq_high, cumsum(c(TRUE, diff(conseq_high$position) != 1)))
-输出
lst1
#$`1`
# variable X6 X7 X8 X9 X10 X11 X12 X13 X14 position rnum
#1 pep_14 L L L L Q L S C S 3 1
#2 pep_15 L L L Q L S C S Y 4 2
#3 pep_16 L L Q L S C S Y A 5 3
#4 pep_17 L Q L S C S Y A G 6 4
#5 pep_18 Q L S C S Y A G Q 7 5
#6 pep_19 L S C S Y A G Q F 8 6
#7 pep_20 S C S Y A G Q F R 9 7
#8 pep_21 C S Y A G Q F R V 10 8
#9 pep_22 S Y A G Q F R V I 11 9

#$`2`
# variable X6 X7 X8 X9 X10 X11 X12 X13 X14 position rnum
#10 pep_136 E L K V E D P F Y 19 10
#11 pep_137 L K V E D P F Y W 20 11
#12 pep_138 K V E D P F Y W V 21 12

#$`3`
# variable X6 X7 X8 X9 X10 X11 X12 X13 X14 position rnum
#13 pep_164 V S V G L V F L F 26 13
#14 pep_165 S V G L V F L F L 27 14
#15 pep_166 V G L V F L F L Q 28 15

#$`4`
# variable X6 X7 X8 X9 X10 X11 X12 X13 X14 position rnum
#16 pep_175 H R L R G K L R A 30 16
#17 pep_176 R L R G K L R A E 31 17
#$`5`
# variable X6 X7 X8 X9 X10 X11 X12 X13 X14 position rnum
#18 pep_223 Y N W L H R R L A 36 18
#19 pep_224 N W L H R R L A G 37 19
数据
conseq_high <- structure(list(variable = c("pep_14", "pep_15", 
"pep_16", "pep_17",
"pep_18", "pep_19", "pep_20", "pep_21", "pep_22", "pep_136",
"pep_137", "pep_138", "pep_164", "pep_165", "pep_166", "pep_175",
"pep_176", "pep_223", "pep_224"), X6 = c("L", "L", "L", "L",
"Q", "L", "S", "C", "S", "E", "L", "K", "V", "S", "V", "H", "R",
"Y", "N"), X7 = c("L", "L", "L", "Q", "L", "S", "C", "S", "Y",
"L", "K", "V", "S", "V", "G", "R", "L", "N", "W"), X8 = c("L",
"L", "Q", "L", "S", "C", "S", "Y", "A", "K", "V", "E", "V", "G",
"L", "L", "R", "W", "L"), X9 = c("L", "Q", "L", "S", "C", "S",
"Y", "A", "G", "V", "E", "D", "G", "L", "V", "R", "G", "L", "H"
), X10 = c("Q", "L", "S", "C", "S", "Y", "A", "G", "Q", "E",
"D", "P", "L", "V", "F", "G", "K", "H", "R"), X11 = c("L", "S",
"C", "S", "Y", "A", "G", "Q", "F", "D", "P", "F", "V", "F", "L",
"K", "L", "R", "R"), X12 = c("S", "C", "S", "Y", "A", "G", "Q",
"F", "R", "P", "F", "Y", "F", "L", "F", "L", "R", "R", "L"),
X13 = c("C", "S", "Y", "A", "G", "Q", "F", "R", "V", "F",
"Y", "W", "L", "F", "L", "R", "A", "L", "A"), X14 = c("S",
"Y", "A", "G", "Q", "F", "R", "V", "I", "Y", "W", "V", "F",
"L", "Q", "A", "E", "A", "G"), position = c(3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 19L, 20L, 21L, 26L, 27L, 28L, 30L,
31L, 36L, 37L), rnum = 1:19), class = "data.frame",
row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16", "17", "18", "19"))

关于r - 按行号将数据框拆分为数据框列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64544067/

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