为什么lapply或l_ply不能转换矩阵?

时间:2015-05-08 08:17:49

标签: r

由于我的NGS数据中有大数据矩阵,我使用l_ply应用乘法来转换列表列表中的信号矩阵。但是,这种转换不适用于原始矩阵不变。有什么问题?

from <- list(id="12345678", name="Gabriele")
message <- "The quick fox"
comments <- list(list(name="Mickey", comment="Hello world!"), list(name="Donald", message="World...hello!"))
big.list <- list(from = from, message = message, comments = comments)
big.list1 <- list(from = from, message = message)

join <- function(lst) {
  if(length(lst$comments) < 1) {
    bnd <- data.frame(id = unlist(lst$from)[1], name = unlist(lst$from)[2], lst$message)
    bnd$cmt.name <- "na"
    bnd$comment <- "na"
    bnd
  } else {
    bnd <- do.call(rbind, lapply(1:length(lst$comments), function(x) {
      id <- unlist(lst$from)[1]
      name <- unlist(lst$from)[2]
      data.frame(id = id, name = name, lst$message)
    }))
    bnd <- cbind(bnd, do.call(rbind, lst$comments))
    names(bnd) <- c("id", "name", "message", "cmt.name", "comment")
    bnd
  }  
}

join(big.list)
#id     name       message cmt.name        comment
#id  12345678 Gabriele The quick fox   Mickey   Hello world!
#id1 12345678 Gabriele The quick fox   Donald World...hello!

join(big.list1)
#id     name   lst.message cmt.name comment
#id 12345678 Gabriele The quick fox       na      na

1 个答案:

答案 0 :(得分:0)

从另一个stackoverflow线程得到答案。 只需使用&lt;&lt; - 但不是&lt; - 因为函数调用中的赋值运算符会改变全局变量的内容。

Update data frame via function doesn't work