Home > Mobile >  How to create new list from nested lists in R
How to create new list from nested lists in R

Time:03-10

I have a some dataframes q1[[i]] and q2[[i]] contain some (i = 19) lists. For example:

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

I want to create list, contain such fragments:

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
...

and calculate norm for each block (for example)

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

and create new normlist for plotting (19 lists, insofar as i = 19).

I try to crete same list, but I get only last normlist:

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")    
  }
}

And I don't know how to get 19 lists contatin two fields for each lists q1[[i]] and q2[[i]], that form a block, there must be such blocks length (q1[[i]]) for each i (length (q1[[i]]) = length (q2[[i]]))?

Code reproducible:

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)

But it's only last list qnorm, there should be 19 such lists and they need to be written in general list.

P.S. As a result, I got the required list, but I can't calculate the norm for each block, I get an empty list at the output... Why?

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)

CodePudding user response:

Perhaps you can try this

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
)
  • Related