2015-10-21 60 views
0

鑑於以下列表的嵌套列表中提取:從增加的長度

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

創建'如果/ else'條件 – akrun

+1

'sapply(F,如果函數(DIM(x)的[1]> 1)×[1,2,4]其他NA(X))' – akrun

+1

這是很好和s imple。 – frostygoat

回答

0

您可以簡單地在上面akrun建議的sapply中添加if條件。

或者,您可以將數據轉換爲大小爲L x M x N的三維數組,其中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/NcoI位,但你可以根據f計算這個)
然後使用數組的前兩個指數來獲取感興趣的元素:

array_3d[1,4,] # (same as sapply(f,'[',1,4)) 
0

大家可以試試

sapply(f, function(x) if(dim(x)[1]>1) x[2,4] else NA)