不适的问题;下标超出范围(并且无法确定问题出在哪里)

时间:2019-05-29 20:14:37

标签: r apply lapply sapply

有人可以帮忙吗!

我正在尝试使用lapply在大列表中循环lstrends(),但是我收到了错误消息,想不出解决办法

错误:

  

“数据[[var]]中的错误:下标超出范围”

代码:

    ## Test data 
  Tissue=c("Normal","Normal","Normal","Normal","Tumor","Tumor","Tumor","Tumor")
    GeneY = c(34,12,11,8,53,32,39,80)
    GeneZ = c(4,7,3,9,44,32,56,77)
    GeneX = c(42,54,59,22,35,12,22,40)
    dvList <- c("GeneY", "GeneZ")
    breast = data.frame(Tissue, GeneX,GeneY,GeneZ)

    ## linear regression lapply works fine
    model = lapply(dvList, function(x) {
        lm(substitute(i~GeneX*Tissue, list(i = as.name(x))), data = breast)})
    library(lsmeans)

    ## selecting individual components of the list works fine
    m.lst = as.data.frame(lstrends(model[[2]], "Tissue", var="GeneX"))

    ## lapply for lstrends doesn't work for me
    m.lst = lapply(model, function(x) {
       as.data.frame(lstrends(model, "Tissue", var="GeneX"))})

1 个答案:

答案 0 :(得分:0)

小错字:

m.lst = lapply(model, function(x) {
  as.data.frame(lstrends(x, "Tissue", var="GeneX"))})

贷记重新兑换用户suspicious_gardener