给出以下列表:
f= list(structure(c(50000, 29277.6814950177, 215.372494674057, 0
), .Dim = c(1L, 4L), .Dimnames = list(NULL, c("", "", "",
""))), structure(c(50000, 1869.78132895568, 27310.9166033304,
1869.78132895568, 230.87438891285, 0, 3739.56265791135, 0), .Dim = c(2L,
4L), .Dimnames = list(c("", ""), c("", "", "", ""))),
structure(c(50000, 1869.78132895568, 3106.11531687008, 25309.4658654162,
1869.78132895568, 3106.11531687008, 246.376283151642, 15.5018942387924,
0, 6212.23063374016, 0, 11), .Dim = 3:4, .Dimnames = list(
c("", "", ""), c("", "alive", "CDD", "Mx"))), structure(c(50000,
1869.78132895568, 3106.11531687008, 5019.5545277528, 23302.2104359498,
1824.92242551596, 3106.11531687008, 5019.5545277528, 261.657500546887,
30.7831116340377, 15.2812173952453, 0, 10039.1090555056,
0, 13, 28), .Dim = c(4L, 4L), .Dimnames = list(c("", "", "",
""), c("", "", "", ""))), structure(c(50000, 1869.78132895568,
3106.11531687008, 5019.5545277528, 7801.28616925366, 21331.1053779996,
1778.13158218736, 3032.68611506447, 5019.5545277528, 7801.28616925366,
277.15939478568, 46.2850058728301, 30.7831116340377, 15.5018942387924,
0, 15602.5723385073, 0, 0, 0, 0), .Dim = c(5L, 4L), .Dimnames = list(
c("", "", "", "", ""), c("", "", "", ""))),
structure(c(50000, 1869.78132895568, 3106.11531687008, 5019.5545277528,
7801.28616925366, 11743.3266592292, 19353.6475682091, 1728.01292394643,
2953.86506288807, 4899.12776533743, 7801.28616925366, 11743.3266592292,
292.661289024472, 61.7869001116225, 46.2850058728301, 31.0037884775848,
15.5018942387924, 0, 23486.6533184584, 0, 80, 0, 44, 0), .Dim = c(6L,
4L), .Dimnames = list(c("", "", "", "", "", ""), c("", "",
"", ""))), structure(c(50000, 1869.78132895568, 3106.11531687008,
5019.5545277528, 7801.28616925366, 11743.3266592292, 16905.8272800675,
17413.7200125228, 1675.19738614863, 2870.60702109872, 4771.64977989233,
7614.12142568036, 11743.3266592292, 16905.8272800675, 308.033783698529,
77.1593947856798, 61.6575005468874, 46.3762831516421, 30.8743889128497,
15.3724946740573, 0, 33811.654560135, 0, 2, 0, 32, 0, 0), .Dim = c(7L,
4L), .Dimnames = list(c("", "", "", "", "", "", ""), c("",
"", "", ""))), structure(c(50000, 1869.78132895568,
3106.11531687008, 5019.5545277528, 7801.28616925366, 11743.3266592292,
16905.8272800675, 23074.9732638488, 15542.5457859789, 1620.15635869262,
2783.61983882035, 4638.15401839932, 7417.69754690023, 11464.0059477916,
16905.8272800675, 23074.9732638488, 323.608460731312, 92.7340718184626,
77.2321775796701, 61.9509601844249, 46.4490659456324, 30.94717170684,
15.5746770327828, 0, 46149.9465276975, 0, 65, 3, 0, 0, 0,
0), .Dim = c(8L, 4L), .Dimnames = list(c("", "", "", "",
"", "", "", ""), c("", "", "", ""))), structure(c(50000,
1869.78132895568, 3106.11531687008, 5019.5545277528, 7801.28616925366,
11743.3266592292, 16905.8272800675, 23074.9732638488, 29861.2718987174,
13715.913714934, 1561.70712465297, 2690.99034114288, 4495.64139408617,
7207.51292128495, 11164.4692321491, 16498.270773798, 23074.9732638488,
29861.2718987174, 339.432885215293, 108.558496302443, 93.0566020636504,
77.7753846684051, 62.2734904296127, 46.7715961908203, 31.399101516763,
15.8244244839803, 0, 59722.5437974349, 0,50, 0, 98, 0, 0,
0, 0), .Dim = c(9L, 4L), .Dimnames = list(c("", "", "", "",
"", "", "", "", ""), c("", "", "", ""))), structure(c(50000,
1869.78132895568, 3106.11531687008, 5019.5545277528, 7801.28616925366,
11743.3266592292, 16905.8272800675, 23074.9732638488, 29861.2718987174,
36339.5645829125, 11947.1836212812, 1499.60998017751, 2592.27911996001,
4343.35054384559, 6982.32430991813, 10842.7835083949, 16059.6225281442,
22509.5093475747, 29861.2718987174, 36339.5645829125, 355.257309699273,
124.382920786423, 108.881026547631, 93.5998091523854, 78.097914913593,
62.5960206748006, 47.2235260007433, 31.6488489679605, 15.8244244839803,
0, 72679.1291658251, 0, 20, 0, 0, 33, 0, 54, 76, 0), .Dim = c(10L,
4L), .Dimnames = list(c("", "", "", "", "", "", "", "", "",
""), c("", "", "", ""))))
我希望在每个子列表中提取相同的元素。例如:
sapply(f,'[',1,4)
正在做我想做的事。然而,
sapply(f,'[',2,4)
在列表增长时失败,并且子列表1上没有第二行。在f [2]之后有第2行,依此类推。我该如何处理这个问题?
答案 0 :(得分:0)
您可以按照上面的akrun建议在sapply中添加if条件。
或者,您可以将数据转换为大小为L x M x N的3d数组,其中L x N为最大子列表的维数,N为列表数。要走的路当然取决于您的应用程序以及执行此操作的频率。
f_padded <- lapply(f, function(x) {
x <- "[<-"(x = matrix(NA, nrow = 10, ncol = 4, byrow = TRUE),
i = 1:nrow(x), j = 1:ncol(x), value = x)
})
array_3d <- abind(f_padded, along = 3)
(我直接输入nrow / ncol,但你可以用f来计算) 然后使用数组的前两个索引来获取感兴趣的元素:
array_3d[1,4,] # (same as sapply(f,'[',1,4))
答案 1 :(得分:0)
我们可以尝试
sapply(f, function(x) if(dim(x)[1]>1) x[2,4] else NA)