【发布时间】:2023-11-09 10:21:02
【问题描述】:
我正在处理一个大型数据框,其中在称为离子分数的列中有许多空行 (NA)。基本上,这个分数是对某些蛋白质的识别。
我的数据框的大体结构是:
N | Accession | Ion Score1 | Ion Score2 | Ion Score3 |
1 | 4322 | 5 | 5 | 5 |
2 | 2344 | 5 | NA | 5 |
3 | 2341 | NA | 5 | NA |
4 | 2346 | NA | NA | 5 |
5 | 2346 | 5 | NA | NA |
6 | 2348 | NA | 5 | 5 |
7 | 2349 | 5 | 5 | NA |
我想要的是这样的:
df1:
N | Accession | Ion Score1 | Ion Score2 | Ion Score3 |
1 | 4322 | 5 | 5 | 5 |
df2:
N | Accession | Ion Score1 | Ion Score2 | Ion Score3 |
2 | 2349 | 5 | 5 | NA |
df3:
N | Accession | Ion Score1 | Ion Score2 | Ion Score3 |
3 | 2344 | 5 | NA | 5 |
df4:
N | Accession | Ion Score1 | Ion Score2 | Ion Score3 |
5 | 2347 | NA | NA | 5 |
.
.
.
等等,考虑到所有可能的组合。
在获取包含所有可能组合的数据表的情况下,一个更具说明性的示例如下:
N | Accession | Ion Score1 | Ion Score2 | Ion Score3 |
1 | 4322 | 5 | 5 | 5 |
2 | 2349 | 5 | 5 | NA |
3 | 2344 | 5 | NA | 5 |
4 | 2348 | NA | 5 | 5 |
5 | 2347 | NA | NA | 5 |
6 | 2341 | NA | 5 | NA |
7 | 2349 | 5 | NA | NA |
为了更清楚,通过这种方式,我可以看到哪些是三个共同的,两个是共同的,哪些只在一个样本中。
我认为最好的方法是在 R 中使用组合。 然后尝试对列进行过滤、排序和分组。
首先我使用它来了解可能有多少种组合
#Where N is the number of combinations, in this case 3.
Combination_table <- data.frame(expand.grid(rep(list(0:1), 19)))
#invert row order
Combination_table <- Combination_table[-nrow(Combination_table), ]
然后按照组合过滤并创建一个新的数据框:
df1 <- data.frame(Proteins[!is.na(Proteins$Ion Score) &
!is.na(Proteins$Ion Score2) &
!is.na(Proteins$Ion Score3), ])
df2 <- data.frame(Proteins[!is.na(Proteins$Ion Score) &
is.na(Proteins$Ion Score2) &
!is.na(Proteins$Ion Score3), ])
df3 <- data.frame(Proteins[!is.na(Proteins$Ion Score) &
!is.na(Proteins$Ion Score2) &
is.na(Proteins$Ion Score3), ])
df4 <- data.frame(Proteins[is.na(Proteins$Ion Score) &
is.na(Proteins$Ion Score2) &
!is.na(Proteins$Ion Score3), ])
.
.
.
等等
这很好用,问题是当我有很多离子分数列时。例如,9 个 Ion Score 列 = 512 种可能的组合。
您知道另一种方法吗?
示例数据集:
Proteins <- data.frame(N = c(1, 2, 3, 4), Accession = c(4322,
222, 2344, 2341), `Ion Score1` = c(5, 5, "NA", "NA"), `Ion Score2` = c(5,
"NA", 5, 5), `Ion Score3` = c(5, 5, "NA", 5))
编辑:
N | Accession | Ion Score1 | Ion Score2 | Ion Score3 |
1 | 4322 | 3 | 51 | 12 |
2 | 4533 | 7 | NA | 87 |
3 | 4125 | NA | 9 | NA |
4 | 8964 | NA | 9 | NA |
5 | 5454 | NA | 10 | NA |
6 | 9871 | 6 | 5 | NA |
7 | 7562 | NA | 5 | NA |
8 | 7894 | 8 | NA | 5 |
9 | 0189 | 5 | NA | NA |
10| 8746 | NA | 45 | 54 |
11| 8746 | 5 | 23 | NA |
例子:
Proteins <- data.frame(N = c(1, 2, 3, 4,5,6,7,8,9,10,11), Accession = c(4322,222, 2344, 2341,6598,98974,7889,78798,1212,4566,1148), `Ion Score1` = c(3, 7, "NA", "NA","NA",6,"NA",8,5,"NA",5), `Ion Score2` = c(51, "NA",9,9,10,5,5,"NA","NA",45, 23), `Ion Score3` = c(12,87,"NA","NA","NA","NA","NA", 5, "NA", 54,"NA"))
【问题讨论】:
-
什么组合?这不是很清楚。请同时发布预期结果
-
@Sotos 组合不重复。我的意思是例如:有这个字母,A,B,C......找到可能的组合。所以,这应该是:ABC、AB、AC 和 BC。这就是我在这个示例中所做的,比较 3 个示例之间的所有可能组合。
-
不,我知道组合是什么 :) 我问的是你在组合什么
-
@Sotos 我正在结合 Ion Score 的数据。该数字表示样品中是否存在蛋白质。如果 Ion Score 列中有数字,则表示存在蛋白质。如果没有 (NA),则表示没有蛋白质。
-
@Sotos 例如,在第一行你可以看到 5 | 5 | 5. 这意味着这三个例子中有一个蛋白质。在下一行你可以看到: 5 | 5 |不适用。这意味着示例 1 和示例 2 中存在蛋白质,但示例 3 中没有。
标签: r combinations multiple-columns