gpt4 book ai didi

r - 根据字符串匹配过滤列表

转载 作者:行者123 更新时间:2023-12-05 09:02:17 26 4
gpt4 key购买 nike

我有一个 list 的小子集,我在其中制作了一个包含三列的多个文件的列表。

我的目标是过滤列表只保留列表中的行,如果它是 NS 那么我想删除列表

head(result_abd[[1]],10)
Symbol log2FoldChange UP_DOWN
1 Gnai3 0.07417434 NS
2 Cdc45 0.30915286 DOWN
3 H19 1.80655193 UP
4 Scml2 -0.99676631 NS
5 Narf -0.66709244 DOWN
6 Cav2 0.14435672 NS
7 Klf6 -0.08168849 DOWN
8 Scmh1 -0.31652589 DOWN
9 Cox5a 0.26321581 NS
10 Wnt9a -0.50397731 NS

我的列表结构是这样的

str(result_abd)
List of 6
$ C1_ref_vs_C2:'data.frame': 15188 obs. of 3 variables:
..$ Symbol : Factor w/ 15187 levels "0610009O20Rik ",..: 4384 1845 4623 11902 6754 1614 5483 11901 2353 14551 ...
..$ log2FoldChange: num [1:15188] 0.0742 0.3092 1.8066 -0.9968 -0.6671 ...
..$ UP_DOWN : chr [1:15188] "NS" "DOWN" "UP" "NS" ...
$ C1_ref_vs_C3:'data.frame': 15188 obs. of 3 variables:
..$ Symbol : Factor w/ 15187 levels "0610009O20Rik ",..: 4384 1845 4623 11902 6754 1614 5483 11901 2353 14551 ...
..$ log2FoldChange: num [1:15188] 0.0555 0.1878 0.8393 -0.5337 -0.8647 ...
..$ UP_DOWN : chr [1:15188] "NS" "DOWN" "NS" "NS" ...
$ C2_ref_vs_C1:'data.frame': 15188 obs. of 3 variables:
..$ Symbol : Factor w/ 15187 levels "0610009O20Rik ",..: 4384 1845 4623 11902 6754 1614 5483 11901 2353 14551 ...
..$ log2FoldChange: num [1:15188] -0.0742 -0.3092 -1.8066 0.9968 0.6671 ...
..$ UP_DOWN : chr [1:15188] "NS" "DOWN" "UP" "NS" ...
$ C2_ref_vs_C3:'data.frame': 15188 obs. of 3 variables:
..$ Symbol : Factor w/ 15187 levels "0610009O20Rik ",..: 4384 1845 4623 11902 6754 1614 5483 11901 2353 14551 ...
..$ log2FoldChange: num [1:15188] -0.0187 -0.1214 -0.9673 0.4631 -0.1976 ...
..$ UP_DOWN : chr [1:15188] "NS" "DOWN" "NS" "NS" ...
$ C3_ref_vs_C1:'data.frame': 15188 obs. of 3 variables:
..$ Symbol : Factor w/ 15187 levels "0610009O20Rik ",..: 4384 1845 4623 11902 6754 1614 5483 11901 2353 14551 ...
..$ log2FoldChange: num [1:15188] -0.0555 -0.1878 -0.8393 0.5337 0.8647 ...
..$ UP_DOWN : chr [1:15188] "NS" "DOWN" "NS" "NS" ...
$ C3_ref_vs_C2:'data.frame': 15188 obs. of 3 variables:
..$ Symbol : Factor w/ 15187 levels "0610009O20Rik ",..: 4384 1845 4623 11902 6754 1614 5483 11901 2353 14551 ...
..$ log2FoldChange: num [1:15188] 0.0187 0.1214 0.9673 -0.4631 0.1976 ...
..$ UP_DOWN : chr [1:15188] "NS" "DOWN" "NS" "NS" ...

对于简单的数据框,我知道如何过滤,但对于列表,我不确定如何去做。任何建议或帮助将不胜感激。

最佳答案

一种选择是使用 lapply 来过滤列表中的数据框,如下所示:

lapply(result_abd, function(x) x[x$UP_DOWN %in% c("UP", "DOWN"), ])
#> $C1_ref_vs_C2
#> Symbol log2FoldChange UP_DOWN
#> 2 Cdc45 0.30915286 DOWN
#> 3 H19 1.80655193 UP
#> 5 Narf -0.66709244 DOWN
#> 7 Klf6 -0.08168849 DOWN
#> 8 Scmh1 -0.31652589 DOWN
#>
#> $C1_ref_vs_C3
#> Symbol log2FoldChange UP_DOWN
#> 2 Cdc45 0.30915286 DOWN
#> 3 H19 1.80655193 UP
#> 5 Narf -0.66709244 DOWN
#> 7 Klf6 -0.08168849 DOWN
#> 8 Scmh1 -0.31652589 DOWN

数据


df <- structure(list(Symbol = c(
"Gnai3", "Cdc45", "H19", "Scml2", "Narf",
"Cav2", "Klf6", "Scmh1", "Cox5a", "Wnt9a"
), log2FoldChange = c(
0.07417434,
0.30915286, 1.80655193, -0.99676631, -0.66709244, 0.14435672,
-0.08168849, -0.31652589, 0.26321581, -0.50397731
), UP_DOWN = c(
"NS",
"DOWN", "UP", "NS", "DOWN", "NS", "DOWN", "DOWN", "NS", "NS"
)), class = "data.frame", row.names = c(
"1",
"2", "3", "4", "5", "6", "7", "8", "9", "10"
))

result_abd <- list(C1_ref_vs_C2 = df, C1_ref_vs_C3 = df)

关于r - 根据字符串匹配过滤列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71715449/

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