如何根据条件对列表进行子集化?

时间:2015-09-02 10:46:12

标签: r subset

SO中针对此问题Subset list based on condition提供的示例在我的列表中不起作用。

我的清单在这里,

dput(SO.Quest)
structure(list(wc010615.csv = structure(list(Cost.Centre = c(71340000L, 
71140000L, 71340001L, 71340005L, 71340006L, 71340006L, 71250000L, 
71340001L, 71340001L, 71340005L, 71250000L, 71250000L, 71250000L, 
71340005L, 71250000L, 71250000L, 71250000L, 71340000L, 71510000L, 
71340000L, 71510000L, 71340000L, 71340000L), Spend.Type = c("Purchase", 
"Purchase", "Purchase", "Purchase", "Stores Issue", "Stores Issue", 
"Purchase", "Purchase", "Purchase", "Purchase", "Purchase", "Purchase", 
"Purchase", "Purchase", "Purchase", "Purchase", "Purchase", "Stores Issue", 
"Stores Issue", "Purchase", "Stores Issue", "Stores Issue", "Stores Issue"
), Quantity = c(12L, 1L, 1L, 5L, 3L, 2L, 3L, 2L, 2L, 1L, 2L, 
2L, 1L, 1L, 1L, 1L, 1L, 3L, 2L, 1L, 3L, 3L, 1L), Total.Cost = c(409.68, 
390, 81.1, 75, 66, 61.42, 43.92, 30, 30, 29.88, 29.28, 29.28, 
18.14, 15, 14.66, 14.64, 14.64, 9.15, 9, 4.6, 3.57, 3.15, 3.05
), Approved = c("01/06/2015 14:44", "04/06/2015 08:33", "02/06/2015 10:49", 
"04/06/2015 14:26", "01/06/2015 11:55", "05/06/2015 09:05", "04/06/2015 14:25", 
"01/06/2015 14:45", "04/06/2015 14:26", "02/06/2015 10:48", "02/06/2015 16:51", 
"02/06/2015 16:52", "02/06/2015 16:52", "05/06/2015 09:05", "04/06/2015 14:25", 
"02/06/2015 16:51", "02/06/2015 16:51", "04/06/2015 08:33", "05/06/2015 14:18", 
"04/06/2015 14:24", "05/06/2015 14:18", "05/06/2015 09:05", "03/06/2015 14:21"
), Requested = c("28/05/2015 16:16", "04/06/2015 08:24", "02/06/2015 09:49", 
"04/06/2015 10:41", "01/06/2015 11:52", "04/06/2015 08:54", "04/06/2015 14:02", 
"01/06/2015 13:40", "03/06/2015 16:08", "02/06/2015 10:37", "02/06/2015 16:40", 
"02/06/2015 16:36", "02/06/2015 16:30", "05/06/2015 08:57", "04/06/2015 14:02", 
"02/06/2015 16:40", "02/06/2015 16:40", "04/06/2015 08:29", "20/05/2015 10:15", 
"04/06/2015 13:51", "20/05/2015 10:15", "03/06/2015 09:05", "03/06/2015 14:03"
), Turnaround..hrs. = c(94.47, 0.15, 1, 3.75, 0.05, 24.18, 0.38, 
1.08, 22.3, 0.18, 0.18, 0.27, 0.37, 0.13, 0.38, 0.18, 0.18, 0.07, 
388.05, 0.55, 388.05, 48, 0.3)), .Names = c("Cost.Centre", "Spend.Type", 
"Quantity", "Total.Cost", "Approved", "Requested", "Turnaround..hrs."
), class = "data.frame", row.names = c(NA, -23L)), wc030815.csv = structure(list(
    Cost.Centre = c(71340000L, 71340006L, 71250000L, 71340005L, 
    71340076L, 71340001L, 71340001L, 71340001L, 71340005L, 71340005L
    ), Spend.Type = c("Purchase", "Purchase", "Purchase", "Purchase", 
    "Purchase", "Purchase", "Purchase", "Purchase", "Purchase", 
    "Purchase"), Quantity = c(1L, 1L, 5L, 2L, 1L, 1L, 1L, 1L, 
    1L, 1L), Total.Cost = c(279.5, 78.3, 73.2, 30, 20, 15, 15, 
    15, 15, 15), Approved = c("03/08/2015 13:33", "05/08/2015 14:11", 
    "04/08/2015 14:06", "04/08/2015 13:56", "04/08/2015 14:39", 
    "05/08/2015 14:07", "04/08/2015 13:56", "07/08/2015 14:15", 
    "07/08/2015 14:14", "03/08/2015 13:33"), Requested = c("03/08/2015 13:29", 
    "05/08/2015 11:35", "04/08/2015 13:05", "04/08/2015 09:57", 
    "04/08/2015 14:27", "05/08/2015 08:45", "04/08/2015 09:39", 
    "06/08/2015 09:44", "07/08/2015 09:39", "03/08/2015 09:54"
    ), Turnaround..hrs. = c(0.07, 2.6, 1.02, 3.98, 0.2, 5.37, 
    4.28, 28.52, 4.58, 3.65)), .Names = c("Cost.Centre", "Spend.Type", 
"Quantity", "Total.Cost", "Approved", "Requested", "Turnaround..hrs."
), class = "data.frame", row.names = c(NA, -10L)), wc060715.csv = structure(list(
    Cost.Centre = c(71340000L, 71340000L, 71340003L, 71340000L, 
    71340003L, 71250000L, 71340076L, 71340076L, 71340005L, 71340005L, 
    71250000L, 71250000L, 71340000L, 71340000L, 71340001L, 71340001L, 
    71340005L, 71340000L, 71340000L, 71340000L), Spend.Type = c("Purchase", 
    "Purchase", "Purchase", "Purchase", "Purchase", "Purchase", 
    "Purchase", "Purchase", "Purchase", "Purchase", "Purchase", 
    "Purchase", "Purchase", "Purchase", "Purchase", "Purchase", 
    "Purchase", "Stores Issue", "Stores Issue", "Stores Issue"
    ), Quantity = c(1L, 1L, 1L, 1L, 1L, 5L, 1L, 1L, 2L, 2L, 2L, 
    2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), Total.Cost = c(360, 
    300, 240, 90, 75, 73.2, 60.9, 42.2, 30, 30, 29.28, 29.28, 
    22.1, 16.5, 15, 15, 15, 10.6, 10.6, 10.17), Approved = c("07/07/2015 16:48", 
    "07/07/2015 16:48", "09/07/2015 17:10", "07/07/2015 16:48", 
    "09/07/2015 17:10", "07/07/2015 16:48", "07/07/2015 16:48", 
    "07/07/2015 16:49", "06/07/2015 13:53", "07/07/2015 10:22", 
    "07/07/2015 16:48", "07/07/2015 16:48", "06/07/2015 13:01", 
    "06/07/2015 13:01", "10/07/2015 09:13", "10/07/2015 10:23", 
    "08/07/2015 10:51", "09/07/2015 09:04", "09/07/2015 09:07", 
    "09/07/2015 08:28"), Requested = c("07/07/2015 14:51", "07/07/2015 14:51", 
    "09/07/2015 14:59", "07/07/2015 14:51", "09/07/2015 14:59", 
    "07/07/2015 15:40", "07/07/2015 16:19", "07/07/2015 16:43", 
    "06/07/2015 10:41", "07/07/2015 09:59", "07/07/2015 15:40", 
    "07/07/2015 15:40", "06/07/2015 12:57", "06/07/2015 12:57", 
    "10/07/2015 09:05", "10/07/2015 10:20", "08/07/2015 10:38", 
    "09/07/2015 08:59", "09/07/2015 09:07", "08/07/2015 16:35"
    ), Turnaround..hrs. = c(1.95, 1.95, 2.2, 1.95, 2.2, 1.13, 
    0.48, 0.1, 3.2, 0.38, 1.13, 1.13, 0.07, 0.07, 0.13, 0.05, 
    0.22, 0.08, 0, 15.88)), .Names = c("Cost.Centre", "Spend.Type", 
"Quantity", "Total.Cost", "Approved", "Requested", "Turnaround..hrs."
), class = "data.frame", row.names = c(NA, -20L)), wc080615.csv = structure(list(
    Cost.Centre = c(71250000L, 71340005L, 71510000L, 71250000L, 
    71510000L, 71340006L, 71340000L, 71340001L, 71340001L, 71250000L, 
    71340000L, 71340006L, 71250000L, 71510000L), Spend.Type = c("Purchase", 
    "Purchase", "Stores Issue", "Purchase", "Stores Issue", "Purchase", 
    "Purchase", "Purchase", "Purchase", "Purchase", "Stores Issue", 
    "Purchase", "Purchase", "Stores Issue"), Quantity = c(1L, 
    4L, 1L, 3L, 1L, 1L, 20L, 2L, 2L, 1L, 1L, 1L, 1L, 2L), Total.Cost = c(185, 
    60, 46.39, 43.92, 36.29, 30.4, 30, 30, 30, 14.66, 11.31, 
    10.3, 5, 3.78), Approved = c("10/06/2015 16:33", "11/06/2015 09:07", 
    "09/06/2015 14:54", "10/06/2015 16:18", "08/06/2015 09:35", 
    "08/06/2015 15:49", "09/06/2015 14:55", "12/06/2015 13:48", 
    "08/06/2015 09:34", "10/06/2015 16:18", "08/06/2015 09:35", 
    "08/06/2015 15:49", "09/06/2015 14:55", "08/06/2015 09:35"
    ), Requested = c("10/06/2015 16:17", "11/06/2015 08:53", 
    "08/06/2015 12:18", "10/06/2015 16:04", "01/06/2015 13:03", 
    "08/06/2015 15:44", "09/06/2015 11:31", "12/06/2015 13:26", 
    "08/06/2015 08:45", "10/06/2015 16:04", "01/06/2015 13:04", 
    "08/06/2015 15:44", "09/06/2015 11:31", "01/06/2015 13:03"
    ), Turnaround..hrs. = c(0.27, 0.23, 26.6, 0.23, 164.53, 0.08, 
    3.4, 0.37, 0.82, 0.23, 164.52, 0.08, 3.4, 164.53)), .Names = c("Cost.Centre", 
"Spend.Type", "Quantity", "Total.Cost", "Approved", "Requested", 
"Turnaround..hrs."), class = "data.frame", row.names = c(NA, 
-14L)), wc100815.csv = structure(list(Cost.Centre = c(71340076L, 
71250000L, 71250000L, 71340000L, 71340007L, 71510000L, 71340076L, 
71250000L, 71250000L, 71340005L), Spend.Type = c("Purchase", 
"Purchase", "Purchase", "Purchase", "Purchase", "Stores Issue", 
"Purchase", "Purchase", "Purchase", "Stores Issue"), Quantity = c(1L, 
4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 4L), Total.Cost = c(133, 58.72, 
58.72, 50.44, 48.5, 21.83, 21.2, 14.68, 14.67, 10.96), Approved = c("11/08/2015 16:44", 
"13/08/2015 15:19", "13/08/2015 15:19", "13/08/2015 10:08", "14/08/2015 12:56", 
"11/08/2015 15:36", "12/08/2015 09:32", "13/08/2015 15:19", "13/08/2015 15:19", 
"13/08/2015 14:04"), Requested = c("11/08/2015 16:37", "12/08/2015 11:44", 
"12/08/2015 11:41", "13/08/2015 10:06", "13/08/2015 15:23", "11/08/2015 15:33", 
"12/08/2015 08:22", "12/08/2015 11:44", "12/08/2015 11:41", "13/08/2015 13:07"
), Turnaround..hrs. = c(0.12, 27.58, 27.63, 0.03, 21.55, 0.05, 
1.17, 27.58, 27.63, 0.95)), .Names = c("Cost.Centre", "Spend.Type", 
"Quantity", "Total.Cost", "Approved", "Requested", "Turnaround..hrs."
), class = "data.frame", row.names = c(NA, -10L)), wc200715.csv = structure(list(
    Cost.Centre = c(71340000L, 71340000L, 71340005L, 71340005L, 
    71340005L, 71340005L, 71340000L), Spend.Type = c("Purchase", 
    "Purchase", "Purchase", "Purchase", "Purchase", "Purchase", 
    "Purchase"), Quantity = c(1L, 1L, 3L, 2L, 2L, 1L, 1L), Total.Cost = c(320L, 
    133L, 45L, 30L, 30L, 15L, 13L), Approved = c("23/07/2015 15:52", 
    "23/07/2015 15:52", "23/07/2015 15:52", "20/07/2015 09:39", 
    "22/07/2015 10:30", "22/07/2015 10:30", "23/07/2015 15:52"
    ), Requested = c("23/07/2015 14:09", "23/07/2015 14:09", 
    "23/07/2015 11:16", "20/07/2015 09:31", "22/07/2015 09:35", 
    "21/07/2015 09:54", "22/07/2015 16:11"), Turnaround..hrs. = c(1.72, 
    1.72, 4.6, 0.13, 0.92, 24.6, 23.68)), .Names = c("Cost.Centre", 
"Spend.Type", "Quantity", "Total.Cost", "Approved", "Requested", 
"Turnaround..hrs."), class = "data.frame", row.names = c(NA, 
-7L)), wc220615.csv = structure(list(Cost.Centre = c(71340003L, 
71340000L, 71340000L, 71340005L, 71340000L, 71250000L, 71340001L, 
71340005L, 71340005L, 71340001L, 71340001L, 71250000L, 71340000L
), Spend.Type = c("Purchase", "Purchase", "Purchase", "Purchase", 
"Purchase", "Purchase", "Purchase", "Purchase", "Purchase", "Purchase", 
"Purchase", "Purchase", "Purchase"), Quantity = c(1L, 2L, 5L, 
5L, 5L, 3L, 2L, 2L, 1L, 1L, 1L, 1L, 1L), Total.Cost = c(700, 
115.02, 91.9, 75, 71.4, 43.92, 30, 30, 15, 15, 15, 14.66, 8.4
), Approved = c("24/06/2015 14:29", "22/06/2015 10:21", "22/06/2015 10:21", 
"23/06/2015 16:12", "22/06/2015 10:21", "22/06/2015 16:11", "22/06/2015 16:37", 
"25/06/2015 11:08", "22/06/2015 16:37", "25/06/2015 10:50", "22/06/2015 10:21", 
"22/06/2015 16:11", "24/06/2015 14:30"), Requested = c("23/06/2015 16:07", 
"19/06/2015 15:57", "19/06/2015 15:57", "23/06/2015 09:20", "19/06/2015 15:57", 
"22/06/2015 15:49", "22/06/2015 16:14", "25/06/2015 11:03", "22/06/2015 10:41", 
"25/06/2015 10:47", "22/06/2015 09:41", "22/06/2015 15:49", "24/06/2015 09:28"
), Turnaround..hrs. = c(22.37, 66.4, 66.4, 6.87, 66.4, 0.37, 
0.38, 0.08, 5.93, 0.05, 0.67, 0.37, 5.03)), .Names = c("Cost.Centre", 
"Spend.Type", "Quantity", "Total.Cost", "Approved", "Requested", 
"Turnaround..hrs."), class = "data.frame", row.names = c(NA, 
-13L)), wc240815.csv = structure(list(Cost.Centre = c(71340076L, 
71340005L, 71340005L, 71340076L, 71250000L, 71340000L, 71340001L, 
71340000L, 71340076L), Spend.Type = c("Purchase", "Purchase", 
"Purchase", "Stores Issue", "Purchase", "Stores Issue", "Purchase", 
"Stores Issue", "Stores Issue"), Quantity = c(2L, 8L, 2L, 1L, 
1L, 400L, 1L, 1L, 1L), Total.Cost = c(596.4, 120, 30, 22, 18.18, 
16, 15, 5.85, 2.74), Approved = c("28/08/2015 15:27", "24/08/2015 13:06", 
"26/08/2015 14:04", "24/08/2015 10:33", "28/08/2015 14:51", "26/08/2015 15:13", 
"27/08/2015 12:13", "26/08/2015 10:13", "24/08/2015 10:33"), 
    Requested = c("28/08/2015 15:09", "24/08/2015 09:38", "25/08/2015 09:50", 
    "24/08/2015 09:07", "28/08/2015 11:20", "26/08/2015 15:07", 
    "27/08/2015 11:58", "26/08/2015 09:55", "24/08/2015 09:07"
    ), Turnaround..hrs. = c(0.3, 3.47, 28.23, 1.43, 3.52, 0.1, 
    0.25, 0.3, 1.43)), .Names = c("Cost.Centre", "Spend.Type", 
"Quantity", "Total.Cost", "Approved", "Requested", "Turnaround..hrs."
), class = "data.frame", row.names = c(NA, -9L)), wc250515.csv = structure(list(
    Cost.Centre = c(71340005L, 71340076L, 71510000L, 71340076L, 
    71340005L, 71510000L, 71340005L, 71340001L, 71250000L, 71340000L, 
    71510000L, 71340076L, 71340005L, 71250000L), Spend.Type = c("Purchase", 
    "Stores Issue", "Stores Issue", "Purchase", "Purchase", "Stores Issue", 
    "Purchase", "Purchase", "Purchase", "Purchase", "Stores Issue", 
    "Purchase", "Purchase", "Purchase"), Quantity = c(7L, 1L, 
    1L, 3L, 3L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), Total.Cost = c(105, 
    86.41, 46.39, 45, 45, 35.04, 30, 30, 29.16, 26.2, 21.83, 
    15, 15, 14.58), Approved = c("26/05/2015 16:15", "27/05/2015 13:18", 
    "28/05/2015 10:19", "28/05/2015 12:55", "27/05/2015 15:19", 
    "28/05/2015 10:19", "26/05/2015 16:16", "26/05/2015 16:16", 
    "26/05/2015 14:48", "29/05/2015 10:00", "28/05/2015 10:19", 
    "29/05/2015 09:59", "28/05/2015 12:54", "26/05/2015 14:48"
    ), Requested = c("22/05/2015 11:21", "27/05/2015 13:17", 
    "28/05/2015 10:18", "28/05/2015 09:02", "27/05/2015 14:59", 
    "28/05/2015 10:18", "26/05/2015 14:00", "26/05/2015 14:00", 
    "22/05/2015 13:40", "29/05/2015 09:51", "28/05/2015 10:18", 
    "29/05/2015 08:46", "28/05/2015 11:29", "22/05/2015 13:40"
    ), Turnaround..hrs. = c(100.9, 0.02, 0.02, 3.88, 0.33, 0.02, 
    2.27, 2.27, 97.13, 0.15, 0.02, 1.22, 1.42, 97.13)), .Names = c("Cost.Centre", 
"Spend.Type", "Quantity", "Total.Cost", "Approved", "Requested", 
"Turnaround..hrs."), class = "data.frame", row.names = c(NA, 
-14L)), wc290615.csv = structure(list(Cost.Centre = c(71340006L, 
71340076L, 71340005L, 71340000L, 71340005L, 71340005L, 71340005L, 
71340001L, 71340000L, 71340001L, 71340001L, 71340001L, 71340005L, 
71340005L, 71340005L, 71340001L, 71340001L, 71340001L), Spend.Type = c("Purchase", 
"Purchase", "Purchase", "Purchase", "Purchase", "Purchase", "Purchase", 
"Purchase", "Purchase", "Purchase", "Purchase", "Purchase", "Purchase", 
"Purchase", "Purchase", "Purchase", "Purchase", "Purchase"), 
    Quantity = c(1L, 1L, 10L, 1L, 8L, 5L, 5L, 5L, 1L, 1L, 2L, 
    2L, 2L, 2L, 1L, 1L, 1L, 1L), Total.Cost = c(545, 170.15, 
    150, 148.87, 120, 75, 75, 75, 37.29, 31.5, 30, 30, 30, 30, 
    15, 15, 15, 15), Approved = c("03/07/2015 08:42", "03/07/2015 16:33", 
    "03/07/2015 10:25", "29/06/2015 17:03", "30/06/2015 11:48", 
    "29/06/2015 17:03", "02/07/2015 10:10", "02/07/2015 10:09", 
    "29/06/2015 17:03", "03/07/2015 08:20", "30/06/2015 11:48", 
    "30/06/2015 11:48", "03/07/2015 14:15", "30/06/2015 11:48", 
    "02/07/2015 10:10", "03/07/2015 10:26", "03/07/2015 10:26", 
    "02/07/2015 10:10"), Requested = c("03/07/2015 08:34", "03/07/2015 14:45", 
    "03/07/2015 08:54", "26/06/2015 13:58", "30/06/2015 09:42", 
    "29/06/2015 09:15", "01/07/2015 09:57", "02/07/2015 09:52", 
    "26/06/2015 13:58", "02/07/2015 12:37", "30/06/2015 11:43", 
    "30/06/2015 09:08", "03/07/2015 13:45", "26/06/2015 10:53", 
    "02/07/2015 08:52", "03/07/2015 09:10", "03/07/2015 10:21", 
    "02/07/2015 08:49"), Turnaround..hrs. = c(0.13, 1.8, 1.52, 
    75.08, 2.1, 7.8, 24.22, 0.28, 75.08, 19.72, 0.08, 2.67, 0.5, 
    96.92, 1.3, 1.27, 0.08, 1.35)), .Names = c("Cost.Centre", 
"Spend.Type", "Quantity", "Total.Cost", "Approved", "Requested", 
"Turnaround..hrs."), class = "data.frame", row.names = c(NA, 
-18L))), .Names = c("wc010615.csv", "wc030815.csv", "wc060715.csv", 
"wc080615.csv", "wc100815.csv", "wc200715.csv", "wc220615.csv", 
"wc240815.csv", "wc250515.csv", "wc290615.csv"))

我的代码在这里,

cond  <- sapply(SO.Quest, function(x) x[1,] == "71340076")
SO.Quest[cond]

给出,

$<NA>
NULL

$<NA>
NULL

但是我希望有一个data.frame()列表,它只包含第一个字段中“71340076”子集化的字段。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

这应该可以胜任(假设SO.Quest是您的structure代码生成的数据框):

lapply(SO.Quest, function(x) x[x[, 1] == "71340076", ])

首先,如果您想获取列表作为输出,则应使用lapply而不是sapply。其次,代码中的x[1, ]查看了每个data.frame的每一行,而您想要检查每个dfs的第一列中的每个条目的条件(因此x[, 1]),然后根据条件是否满足进行子集化。