簡體   English   中英

如何從 R 中的嵌套列表創建新列表

[英]How to create new list from nested lists in R

我有一些數據幀 q1[[i]] 和 q2[[i]] 包含一些 (i = 19) 列表。 例如:

q1
[[1]]
  [1] 240.13777778 273.73777778 172.73555556  53.70444444 141.80000000 582.93333333 
[[2]]
 [1] 2.409867e+02 2.731156e+02 1.680622e+02 5.300222e+01 5.112444e+01 1.048476e+03
...
q2
[[1]]
  [1]  70.29000000  69.57666667  48.82000000  22.19000000  31.44666667 143.34000000 
[[2]]
 [1]  70.2066667  69.5533333  47.9766667  22.0866667  14.0000000 270.3766667

我想創建列表,包含這樣的片段:

qw1
[[1]]
  [1] 240.13777778
  [1]  70.29000000
[[1]]
  [2] 273.73777778
  [2]  69.57666667

qw2
[[2]]
  [1] 2.409867e+02
  [1]  70.2066667
[[2]]
  [2] 2.731156e+02
  [2]  69.5533333
...

並計算每個塊的范數(例如)

qw2
[[2]]
  [1] 2.409867e+02   -> norm
  [1]  70.2066667
...
[[2]]
  [2] 2.731156e+02   -> norm
  [2]  69.5533333

並為繪圖創建新的規范列表(19 個列表,只要 i = 19)。

我嘗試創建相同的列表,但我只得到最后一個規范列表:

for (i in 1:19){
  q1[[i]] <- dfL_F[[assemble_normal[i]]]/0.000450
  q2[[i]] <- dfL_RMF[[assemble_normal[i]]]/0.000300
  q3[[i]] <- dfL_D[[assemble_normal[i]]]/0.001800
  q4[[i]] <- dfL_RMD[[assemble_normal[i]]]/0.001200
  
  length(q1[[i]])
  length(q2[[i]])
  length(q3[[i]])
  length(q4[[i]])
  qw1 <- lapply(q1[[i]], `[[`, 1)
  qw2 <- lapply(q2[[i]], `[[`, 1)
  qw3 <- lapply(q3[[i]], `[[`, 1)
  qw4 <- lapply(q4[[i]], `[[`, 1)
  
  nn <- list()
  for (j in 1:length(q1[[i]])){
    nn[[j]] <- c(qw1[j],qw2[j],qw3[j],qw4[j])
  }
  
  qnorm1 <- list()
  for (k in 1:length(nn)){
    qnorm1[[k]] <- norm(do.call(rbind, lapply(nn[k], as.numeric)),type = "i")    
  }
}

而且我不知道如何為每個列表 q1[[i]] 和 q2[[i]] 獲取包含兩個字段的 19 個列表,它們形成一個塊,必須有這樣的塊長度 (q1[[i]])對於每個 i(長度(q1[[i]])=長度(q2[[i]]))?

代碼可重現:

dput(q1)
list(c(240.137777777778, 273.737777777778, 172.735555555556, 
53.7044444444444, 141.8, 582.933333333333),c(240.986666666667, 273.115555555556, 168.062222222222,  53.0022222222222, 51.1244444444444, 1048.47555555556)

dput(q2)
list(c(70.29, 69.5766666666667, 48.82, 22.19, 31.4466666666667, 
143.34),c(70.2066666666667, 69.5533333333333, 47.9766666666667, 22.0866666666667, 14, 270.376666666667)

dput(qnorm1)
list(305.738611111111, 365.616666666667, 666.443055555556, 608.981111111111, 393.538611111111, 142.288055555556)

但它只是最后一個列表qnorm,應該有19個這樣的列表,它們需要寫在通用列表中。

PS 結果,我得到了所需的列表,但我無法計算每個塊的范數,我在 output 處得到一個空列表......為什么?

qw <- Map(
   function(q1i, q2i) {
      stopifnot(length(q1i) == length(q2i))
      Map(c, q1i, q2i) # j elementh i block q1[[i]][j], q2[[i]][j]
   },
   q1, q2 # every block conatin q1[[i]], q2[[i]]
)
# list qw conatin blocks qw1, qw2

stopifnot(length(qw1) == length(qw2))
qnorm11 <- Map(
  function(qw1, qw2, qw3, qw4) 
  {
    stopifnot(length(qw1) == length(qw2))
    Map(c, (norm(as.matrix(unlist(qw1),type = "1"))), 
        (norm(as.matrix(unlist(qw2),type = "1"))), 
        (norm(as.matrix(unlist(qw3),type = "1"))), 
        (norm(as.matrix(unlist(qw4),type = "1"))))
  }, qw1, qw2, qw3, qw4)

也許你可以試試這個

list2env(
  setNames(
    Map(function(x, y) apply(rbind(x, y), 2, function(v) norm(t(v)), simplify = FALSE), q1, q2),
    c("qw1", "qw2")
  ),
  envir = .GlobalEnv
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM