【问题标题】:Getting a group of specific entries from a data frame从数据框中获取一组特定条目
【发布时间】:2022-11-14 17:16:56
【问题描述】:

我有一个数据框,其中有一列 row 带有行索引,一列 col 带有列索引。

indexes_df <- structure(list(row = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 13L, 14L, 15L, 16L, 1L, 3L, 6L, 8L, 11L, 12L, 8L, 8L, 
5L, 8L, 8L, 16L, 8L, 16L, 8L, 8L, 16L, 5L, 8L, 16L, 1L, 4L, 6L, 
14L, 15L, 16L, 15L, 1L, 2L, 5L, 6L, 8L, 10L, 15L, 1L, 2L, 5L, 
6L, 8L, 10L, 13L, 15L, 1L, 5L, 6L, 7L, 8L, 10L, 13L, 15L, 1L, 
2L, 5L, 7L, 8L, 13L, 1L, 5L, 6L, 7L, 8L, 10L, 13L, 15L, 2L, 5L, 
6L, 8L, 10L, 13L, 15L), col = c(6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 17L, 17L, 17L, 17L, 17L, 17L, 
18L, 19L, 20L, 20L, 21L, 21L, 22L, 22L, 23L, 24L, 24L, 25L, 25L, 
25L, 43L, 43L, 43L, 43L, 43L, 43L, 53L, 100L, 100L, 100L, 100L, 
100L, 100L, 100L, 108L, 108L, 108L, 108L, 108L, 108L, 108L, 108L, 
109L, 109L, 109L, 109L, 109L, 109L, 109L, 109L, 112L, 112L, 112L, 
112L, 112L, 112L, 121L, 121L, 121L, 121L, 121L, 121L, 121L, 121L, 
128L, 128L, 128L, 128L, 128L, 128L, 128L)), class = "data.frame", row.names = c(NA, 
-87L))

这些索引属于另一个数据框中的(“可疑”)条目(见下文)。我想为每一列找到一种方法,以获取行号的条目并将它们放入表或数据框中。

例如,第 25 列是Incub_25。第 5、8 和 16 行有问题。我想获取 [5,25], [8,25] and [16,25] 并将这些条目放在 Incub_25

Incub_25
9
10
2

我正在想办法编写语法来做到这一点。我首先必须从col 中的所有唯一值创建列,然后将每个条目放在下面。

但我来不及了。有人可以帮我开始如何做到这一点吗?

DPUT 目标_df

请注意,由于某种原因,下面的dput 对我不起作用,but I checked with a third person 可以毫无问题地运行dput

target_df <- structure(list(Incub_1 = c(0.385196293090574, 1.11942931199418, 
0.204807262758973, -0.658873075589505, -1.23526854978509, 0.304188863339875, 
1.9624144441043, -0.964648271693746, 0.326228882549623, 0.882659575810153, 
-0.876661591947629, 0.620359344297636, -0.757983464647292, 1.12239728977615, 
1.71009398297967, -1.58696927045269), Incub_2 = c(-0.0414632979748666, 
0.296547049458993, 1.71909288891566, 0.518769534446091, -0.624908719260206, 
0.601262573635932, 0.469909085365189, -0.664675254364598, -0.440454285558811, 
-1.07950749999069, 1.0059842309736, 1.08189298333815, -0.667124626455829, 
-0.423829554001461, 0.00150373558190289, 1.88126771405575), Incub_3 = c(0.793991364919319, 
1.20307801797634, -0.0737930243600645, 0.523966041771911, 1.69422525823244, 
0.453199056517207, 0.353528213847739, -0.735489729202925, -1.36597876954093, 
1.80789040434975, -0.789466064046612, -0.324545723958204, -2.44569102445658, 
-0.500861487189826, -0.493791835935263, 1.68043918952518), Incub_4 = c(-1.18020105490573, 
-1.08512266252087, 1.97879923168689, 1.1306583336137, 1.14412583281056, 
1.38508255909683, 1.18664910668881, -0.256958141033041, -0.29050820311122, 
-0.59216215209202, 1.71637902072828, 1.14829900721385, -0.479138994377477, 
-0.438553465256564, -0.119020860341265, -0.031698340453245), 
    Incub_5 = c(-0.20304078968779, -0.357916799052825, 0.17277250945184, 
    -0.246603798203555, -0.849815214014788, 2.24197498853988, 
    -0.838647918810149, -1.2043883385906, -1.57422933887106, 
    0.876993752725877, 1.03498477948996, 0.302085601261494, 0.434192568904241, 
    -1.12092791465893, 0.396233192639553, -0.989552665811105), 
    Incub_6 = structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 
    1, 1, 1, 1), label = NA, class = c("labelled", "numeric")), 
    Incub_7 = structure(c(1, 3, 3, 3, 3, 2, 4, 4, 4, 2, 4, 4, 
    3, 4, 3, 2), label = NA, class = c("labelled", "numeric")), 
    Incub_8 = structure(c(NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_), levels = character(0), class = c("labelled", 
    "factor"), label = NA), Incub_9 = structure(c(12, 18, 65, 
    20, 10, 20, 22, 300, 16, 9, 23, 40, 9, 25, 12, 7), label = NA, class = c("labelled", 
    "numeric")), Incub_10 = structure(c(8, 7, 16, 10, 6, 10, 
    12, 175, 8, 6, 17, 26, 4, 11, 8, 4), label = NA, class = c("labelled", 
    "numeric")), Incub_11 = structure(c(3, 17, 47, 10, 5, 17, 
    5, 100, 16, 2, 16, 20, 6, 24, 10, 5), label = NA, class = c("labelled", 
    "numeric")), Incub_12 = structure(c(NA, NA, NA, 1, NA, NA, 
    NA, NA, 1, NA, NA, NA, NA, 1, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_13 = structure(c(NA, NA, NA, 2, 2, 2, 
    2, NA, 2, 2, NA, NA, 2, 2, 2, 2), label = NA, class = c("labelled", 
    "numeric")), Incub_14 = structure(c(NA, NA, NA, NA, NA, NA, 
    NA, NA, 3, 3, NA, 3, NA, NA, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_15 = structure(c(NA, NA, 4, NA, NA, 4, 
    NA, NA, 4, NA, NA, NA, 4, 4, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_16 = structure(c(NA, 5, NA, 5, 5, NA, 
    NA, NA, 5, NA, NA, NA, NA, NA, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_17 = structure(c(3, NA, 6, NA, NA, 2, 
    NA, 1, NA, NA, 4, 5, NA, NA, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_18 = structure(c(NA, 7, 2, 250, 5, 0, 
    0, NA, NA, 10, NA, 150, 22, 27, 2, 650), label = NA, class = c("labelled", 
    "numeric")), Incub_19 = structure(c(NA, 4, 0, 100, NA, 0, 
    0, NA, NA, 9, NA, 40, 5, 27, 0, 350), label = NA, class = c("labelled", 
    "numeric")), Incub_20 = structure(c(NA, 7, 1, 4, 8, 1, 1, 
    9, NA, 2, NA, 3, 7, 5, 1, 6), label = NA, class = c("labelled", 
    "numeric")), Incub_21 = structure(c(NA, 5, 1, 4, NA, 1, 1, 
    9, NA, 8, NA, 7, 2, 3, 2, 6), label = NA, class = c("labelled", 
    "numeric")), Incub_22 = structure(c(NA, 6, 2, 4, NA, 1, 1, 
    10, NA, 7, NA, 9, 1, 3, 5, 8), label = NA, class = c("labelled", 
    "numeric")), Incub_23 = structure(c(0, 11, 1, 175, 4, 0, 
    5, NA, NA, 15, NA, 65, 3, 97, 1, 1175), label = NA, class = c("labelled", 
    "numeric")), Incub_24 = structure(c(NA, 7, 1, 9, NA, 1, 4, 
    10, NA, 3, NA, 5, 2, 6, 1, 8), label = NA, class = c("labelled", 
    "numeric")), Incub_25 = structure(c(NA, 7, 1, 8, 9, 1, 5, 
    10, NA, 3, NA, 6, 1, 4, 1, 2), label = NA, class = c("labelled", 
    "numeric")), Incub_26 = structure(c(0, 5, 0, 175, 1, 0, 1, 
    0, NA, 9, NA, 25, 0, 27, 0, 650), label = NA, class = c("labelled", 
    "numeric")), Incub_27 = structure(c(6, 5, 6, 4, 1, 1, 5, 
    6, NA, 5, NA, 5, 1, 5, 6, 5), label = NA, class = c("labelled", 
    "numeric")), Incub_28 = structure(c(NA, 3, 0, 80, 2, 0, 1, 
    NA, NA, 15, NA, 25, 2, 27, 0, 1175), label = NA, class = c("labelled", 
    "numeric")), Incub_29 = structure(c(NA, 4, 6, 3, 2, 6, 5, 
    6, NA, 5, NA, 4, 1, 5, 6, 4), label = NA, class = c("labelled", 
    "numeric")), Incub_30 = structure(c(0, 5, 2, NA, 6, NA, 1, 
    NA, NA, 14, NA, 25, 1, 27, 1, 600), label = NA, class = c("labelled", 
    "numeric")), Incub_31 = structure(c(NA, 1, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, 1, NA, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_32 = structure(c(NA, 2, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_33 = structure(c(NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, 1, NA, NA, NA, NA, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_34 = structure(c(NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_), levels = character(0), class = c("labelled", 
    "factor"), label = NA), Incub_35 = structure(c(7, 3, 2, 4, 
    2, 7, 5, 7, NA, 5, NA, 5, 1, 3, 1, 4), label = NA, class = c("labelled", 
    "numeric")), Incub_36 = structure(c(1, 3, 4, 3, 2, 1, 5, 
    7, NA, 6, NA, 6, 3, 5, 5, 4), label = NA, class = c("labelled", 
    "numeric")), Incub_37 = structure(c(0, 6, 0, 80, 1, 0, 2, 
    NA, NA, 2, NA, 0, 0, 1, 0, 0), label = NA, class = c("labelled", 
    "numeric")), Incub_38 = structure(c(NA, 1, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, 1, NA, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_39 = structure(c(NA, 2, NA, NA, 2, NA, 
    2, NA, NA, 2, NA, NA, NA, NA, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_40 = structure(c(NA, NA, NA, NA, 3, NA, 
    3, NA, NA, NA, NA, NA, NA, NA, 3, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_41 = structure(c(NA, 4, NA, NA, 4, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_42 = structure(c(NA, NA, 5, NA, NA, NA, 
    NA, NA, NA, NA, NA, 5, NA, NA, NA, 5), label = NA, class = c("labelled", 
    "numeric")), Incub_43 = structure(c(3, NA, NA, 4, NA, 2, 
    NA, NA, NA, NA, NA, NA, NA, 1, 6, 5), label = NA, class = c("labelled", 
    "numeric")), Incub_44 = structure(c(4, 5, 2, 5, 5, 5, 4, 
    5, NA, 5, NA, 1, 4, 5, 3, 5), label = NA, class = c("labelled", 
    "numeric")), Incub_45 = structure(c(NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_), levels = character(0), class = c("labelled", 
    "factor"), label = NA), Incub_46 = structure(c(NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
    ), levels = character(0), class = c("labelled", "factor"), label = NA), 
    Incub_47 = structure(c(NA, NA, 3, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, 3, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_48 = structure(c(NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_), levels = character(0), class = c("labelled", 
    "factor"), label = NA), Incub_49 = structure(c(NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
    ), levels = character(0), class = c("labelled", "factor"), label = NA), 
    Incub_50 = structure(c(NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_), levels = character(0), class = c("labelled", 
    "factor"), label = NA), Incub_51 = structure(c(NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
    ), levels = character(0), class = c("labelled", "factor"), label = NA), 
    Incub_52 = structure(c(NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_), levels = character(0), class = c("labelled", 
    "factor"), label = NA), Incub_53 = structure(c(NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_54 = structure(c(5, 5, NA, NA, 5, 5, 3, 
    3, NA, 5, NA, NA, 3, 5, 4, 5), label = NA, class = c("labelled", 
    "numeric")), Incub_55 = structure(c(1, 2, NA, NA, NA, 2, 
    3, 2, NA, 1, NA, NA, 3, 1, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_56 = structure(c(5, 3, NA, NA, NA, 4, 
    7, 4, NA, 5, NA, NA, 3, 5, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_57 = structure(c(5, 7, NA, NA, 4, 5, 4, 
    4, NA, 5, NA, NA, 3, 5, 2, 2), label = NA, class = c("labelled", 
    "numeric")), Incub_58 = structure(c(1, 6, NA, NA, NA, 2, 
    3, 2, NA, 1, NA, NA, 3, 1, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_59 = structure(c(5, 7, NA, NA, NA, 4, 
    7, 3, NA, 5, NA, NA, 3, 5, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_60 = structure(c(7, 2, NA, NA, 4, 7, 1, 
    3, NA, 4, NA, NA, 2, 3, 5, 2), label = NA, class = c("labelled", 
    "numeric")), Incub_61 = structure(c(6, 3, NA, NA, NA, 6, 
    3, 2, NA, 3, NA, NA, 2, 3, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_62 = structure(c(7, 1, NA, NA, NA, 7, 
    7, 3, NA, 4, NA, NA, 4, 3, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_63 = structure(c(3, 5, NA, NA, 5, 3, 4, 
    2, NA, 4, NA, NA, 4, 5, 3, 5), label = NA, class = c("labelled", 
    "numeric")), Incub_64 = structure(c(3, 2, NA, NA, NA, 3, 
    3, 2, NA, 1, NA, NA, 2, 1, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_65 = structure(c(4, 3, NA, NA, NA, 3, 
    7, 3, NA, 4, NA, NA, 4, 5, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_66 = structure(c(3, 5, NA, NA, 5, 3, 4, 
    4, NA, 5, NA, NA, 3, 5, 5, 5), label = NA, class = c("labelled", 
    "numeric")), Incub_67 = structure(c(2, 2, NA, NA, NA, 2, 
    3, 2, NA, 2, NA, NA, 3, 1, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_68 = structure(c(4, 3, NA, NA, NA, 4, 
    7, 4, NA, 4, NA, NA, 2, 5, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_69 = structure(c(5, 5, NA, NA, 4, 2, 3, 
    5, NA, 4, NA, NA, 5, 4, 5, 5), label = NA, class = c("labelled", 
    "numeric")), Incub_70 = structure(c(2, 2, NA, NA, NA, 1, 
    1, 2, NA, 3, NA, NA, 2, 2, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_71 = structure(c(4, 3, NA, NA, NA, 3, 
    7, 4, NA, 3, NA, NA, 4, 4, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_72 = structure(c(4, 5, NA, NA, 5, 2, 5, 
    5, NA, 4, NA, NA, 3, 5, 4, 5), label = NA, class = c("labelled", 
    "numeric")), Incub_73 = structure(c(2, 2, NA, NA, NA, 2, 
    3, 2, NA, 1, NA, NA, 3, 1, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_74 = structure(c(4, 3, NA, NA, NA, 3, 
    7, 3, NA, 4, NA, NA, 2, 5, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_75 = structure(c(4, 5, NA, NA, 4, 3, 3, 
    5, NA, 5, NA, NA, 4, 5, 5, 5), label = NA, class = c("labelled", 
    "numeric")), Incub_76 = structure(c(2, 2, NA, NA, NA, 2, 
    3, 2, NA, 1, NA, NA, 2, 1, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_77 = structure(c(4, 4, NA, NA, NA, 3, 
    7, 2, NA, 5, NA, NA, 5, 5, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_78 = structure(c(3, 3, NA, NA, 4, 3, 2, 
    1, NA, 4, NA, NA, 2, 2, 2, 5), label = NA, class = c("labelled", 
    "numeric")), Incub_79 = structure(c(3, 2, NA, NA, NA, 2, 
    3, 2, NA, 2, NA, NA, 3, 3, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_80 = structure(c(3, 5, NA, NA, NA, 3, 
    7, 3, NA, 3, NA, NA, 2, 3, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_81 = structure(c(4, 5, NA, NA, 4, 3, 3, 
    5, NA, 5, NA, NA, 5, 5, 4, 4), label = NA, class = c("labelled", 
    "numeric")), Incub_82 = structure(c(2, 2, NA, NA, NA, 2, 
    3, 2, NA, 3, NA, NA, 1, 1, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_83 = structure(c(4, 3, NA, NA, NA, 3, 
    7, 2, NA, 3, NA, NA, 5, 5, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_84 = structure(c(3, 4, NA, NA, 5, 3, 5, 
    4, NA, 5, NA, NA, 4, 5, 5, 5), label = NA, class = c("labelled", 
    "numeric")), Incub_85 = structure(c(3, 2, NA, NA, NA, 2, 
    3, 2, NA, 1, NA, NA, 2, 1, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_86 = structure(c(3, 4, NA, NA, NA, 3, 
    7, 2, NA, 4, NA, NA, 3, 5, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_87 = structure(c(3, 7, NA, NA, 5, 3, 3, 
    3, NA, 4, NA, NA, 7, 3, 2, 5), label = NA, class = c("labelled", 
    "numeric")), Incub_88 = structure(c(3, 6, NA, NA, NA, 2, 
    3, 2, NA, 3, NA, NA, 6, 2, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_89 = structure(c(3, 7, NA, NA, NA, 3, 
    7, 2, NA, 3, NA, NA, 7, 3, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_90 = structure(c(7, 4, NA, NA, 5, 3, 4, 
    3, NA, 5, NA, NA, 5, 5, 4, 4), label = NA, class = c("labelled", 
    "numeric")), Incub_91 = structure(c(6, 2, NA, NA, NA, 2, 
    3, 2, NA, 1, NA, NA, 2, 1, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_92 = structure(c(7, 2, NA, NA, NA, 3, 
    7, 3, NA, 4, NA, NA, 4, 5, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_93 = structure(c(3, 5, NA, NA, 4, 3, 5, 
    5, NA, 5, NA, NA, 4, 5, 5, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_94 = structure(c(3, 2, NA, NA, NA, 2, 
    3, 2, NA, 3, NA, NA, 2, 3, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_95 = structure(c(3, 3, NA, NA, NA, 3, 
    7, 4, NA, 3, NA, NA, 4, 4, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_96 = structure(c(4, 4, NA, NA, 4, 3, 4, 
    5, NA, 5, NA, NA, 4, 5, 4, 5), label = NA, class = c("labelled", 
    "numeric")), Incub_97 = structure(c(2, 2, NA, NA, NA, 2, 
    3, 1, NA, 2, NA, NA, 2, 1, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_98 = structure(c(4, 3, NA, NA, NA, 3, 
    7, 4, NA, 3, NA, NA, 3, 3, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_99 = structure(c(5, 5, NA, 1, 3, 3, 1, 
    3, NA, 3, NA, NA, 2, 5, 4, 5), label = NA, class = c("labelled", 
    "numeric")), Incub_100 = structure(c(6, 1, NA, NA, 7, 3, 
    NA, 2, NA, 4, NA, NA, NA, NA, 5, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_101 = structure(c(5, 4, NA, NA, 3, 2, 
    NA, 4, NA, 4, NA, NA, NA, 3, 4, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_102 = structure(c(4, 5, NA, 5, 4, 4, 5, 
    5, NA, 5, NA, NA, 3, 5, 4, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_103 = structure(c(4, 5, NA, 5, 1, 4, 5, 
    5, NA, 5, NA, NA, 5, 5, 4, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_104 = structure(c(3, 5, NA, 4, 4, 2, 1, 
    4, NA, 5, NA, NA, 3, 3, 4, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_105 = structure(c(4, 5, NA, 5, 3, 4, 4, 
    4, NA, 5, NA, NA, 5, 5, 5, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_106 = structure(c(5, 5, NA, 4, 2, 4, 4, 
    4, NA, 5, NA, NA, 5, 5, 5, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_107 = structure(c(5, 5, NA, 4, 2, 4, 4, 
    4, NA, 5, NA, NA, 5, 5, 5, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_108 = structure(c(7, 3, NA, NA, 8, 1, 
    NA, 2, NA, 6, NA, NA, 4, NA, 5, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_109 = structure(c(4, NA, NA, NA, 8, 2, 
    7, 6, NA, 5, NA, NA, 3, NA, 1, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_110 = structure(c(1, 5, NA, 3, 1, 1, 4, 
    3, NA, 5, NA, NA, 5, 5, 5, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_111 = structure(c(4, 4, NA, 1, 3, 2, 3, 
    4, NA, 1, NA, NA, 4, 5, 1, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_112 = structure(c(5, 1, NA, NA, 4, NA, 
    2, 6, NA, NA, NA, NA, 3, NA, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_113 = structure(c(3, 4, NA, NA, 1, NA, 
    1, 5, NA, NA, NA, NA, 4, 5, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_114 = structure(c(1, 1, NA, 1, NA, 1, 
    1, 1, NA, NA, NA, NA, 1, 1, 1, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_115 = structure(c(NA, 2, NA, 2, 2, 2, 
    2, 2, NA, NA, NA, NA, NA, NA, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_116 = structure(c(NA, 3, NA, NA, 3, 3, 
    3, 3, NA, 3, NA, NA, NA, NA, 3, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_117 = structure(c(NA, NA, NA, NA, 4, 4, 
    NA, 4, NA, NA, NA, NA, NA, 4, 4, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_118 = structure(c(NA, NA, NA, NA, 5, 5, 
    5, 5, NA, 5, NA, NA, 5, 5, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_119 = structure(c(NA, NA, NA, NA, NA, 
    NA, NA, 6, NA, NA, NA, NA, NA, NA, NA, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_120 = structure(c(NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_), levels = character(0), class = c("labelled", 
    "factor"), label = NA), Incub_121 = structure(c(6, NA, NA, 
    NA, 7, 4, 1, 2, NA, 3, NA, NA, 5, NA, 8, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_122 = structure(c(4, 2, NA, 2, 5, 1, 3, 
    5, NA, 5, NA, NA, 5, 5, 5, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_123 = structure(c(3, 5, NA, 2, 5, 2, 2, 
    5, NA, 5, NA, NA, 4, 3, 5, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_124 = structure(c(3, 4, NA, 2, 5, 2, 3, 
    3, NA, 5, NA, NA, 4, 3, 4, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_125 = structure(c(4, 2, NA, 2, 5, 1, 3, 
    5, NA, 5, NA, NA, 5, 5, 5, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_126 = structure(c(4, 5, NA, 2, 5, 2, 4, 
    5, NA, 4, NA, NA, 5, 5, 5, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_127 = structure(c(4, 5, NA, 2, 5, 1, 4, 
    5, NA, 5, NA, NA, 5, 5, 5, NA), label = NA, class = c("labelled", 
    "numeric")), Incub_128 = structure(c(NA, 5, NA, NA, 6, 3, 
    NA, 2, NA, 1, NA, NA, 7, NA, 4, NA), label = NA, class = c("labelled", 
    "numeric"))), row.names = c(NA, -16L), class = c("data.table", 
"data.frame"))

【问题讨论】:

    标签: r lapply


    【解决方案1】:

    我认为一个可能有效:

    tcols <- unique(indexes_df$col)
    tcols <- set_names(tcols, colnames(target_df)[tcols])
    
    ret <- map(tcols, ~ {
        trows <- indexes_df %>% filter(col == .x) %>% pull(row)
        target_df %>% 
             mutate("row"=row_number()) %>% 
             select(row, all_of(.x)) %>% 
             filter(row_number() %in% trows)
    })                                               
    

    ret$Incub_25

     │row  │Incub_25
    1│    5│       9
    2│    8│      10
    3│   16│       2
    

    一些解释:我将 tcols(目标 cols)创建为命名向量,因为您希望结果由列名命名。

    在 map 函数中,我首先只选择indexes_df 中与给定列(即 tcols 的值)相对应的行,并将它们作为称为 trows 的向量拉出。

    然后,我取 target_df,添加行号(只是为了能够仔细检查结果,可以将其删除),同时选择列 row 和列 .x(tcols 的给定值),最后只保留行号这是成排的。

    或者,更简单,如果您不想保留行号:

    ret <- map(tcols, ~ {
        trows <- indexes_df %>% filter(col == .x) %>% pull(row)
        target_df[ trows, .x, drop=F]
    })
    

    您还可以使用非 tidyverse 版本:

    tcols <- unique(indexes_df$col)
    names(tcols) <- colnames(target_df)[tcols]
    
    ret <- lapply(tcols, (x) {
       trows <- indexes_df$row[ indexes_df$col == x ]
       target_df[ trows, x, drop=F]
    })
    

    【讨论】:

    • 这太棒了,非常感谢!
    猜你喜欢
    • 2022-12-15
    • 1970-01-01
    • 2022-12-12
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 2016-05-16
    • 2020-06-25
    相关资源
    最近更新 更多