是否有R函数可将内容添加到现有列表中?

时间:2019-04-15 08:23:42

标签: r

我是R的新手。我正在尝试在2个函数中创建一个列表。第一个是提取功能,它获取数据并创建一个列表。第二个是过程一,它计算一些值,我需要将它们放在第一个列表中。我怎么做 ?

myfun <- function(data,number_meta) { #extraction function
  OR  <- data$`Odds Ratio`[data$`Identification number`==number_meta]
  SE  <- ((log(data$`Upper limit`) - (log(data$`Lower limit`))) / 3.92)[data$`Identification number`==number_meta]
  res <- metagen(TE=log(OR),seTE=SE,sm="OR")

  tableau = cbind(OR, SE)
  LIST = list(tableau, res)

  return(LIST)
}

myfun(data,number_meta) 
number_meta = c(1:33)
i = c(1:33)
number_meta = i
LIST = list()

for (i in 1:33) {
  LIST[[i]] = myfun(data, number_meta[i])
}

myfun2 <- function(LIST) { # processing function
  dup_OR <- duplicated(LIST[[i]][[1]][,1])
  dup_SE <- duplicated(LIST[[i]][[1]][,2])

  options(scipen = 999)

  Egger  <- metabias(LIST[[i]][[2]], method.bias = "linreg", k.min = 1)
  Begg   <- metabias(LIST[[i]][[2]], method.bias = "rank",   k.min = 1)
  Result <- c(dup_OR,dup_SE,Egger,Begg)

  return(Result)
}

myfun2(LIST)

for (i in 1:33) {
  LIST[[i]] = c(LIST, list(myfun2(LIST))) ## This one is not working !
}

我想获得33个项目的最终清单,在这些清单中我可以找到不同的res值(dup_OR,dup_SE,Egger和Begg)。这些值与res的值不同。谢谢您的帮助

这是我的原始脚本:

setwd("U:/Stage M2 Phame")

library(readxl)

library(meta)

data <- read_excel("Tableau_OR.xlsx")

OR <- ((data$`Odds Ratio`[data$`Identification number`==number_meta[i]]))

SE <- (((log(data$`Upper limit`) - (log(data$`Lower limit`)))/3.92)[data$`Identification 
number`==number_meta[i]])

dup_OR <- duplicated(OR)

dup_SE <- duplicated(SE)

options(scipen = 999)

res <- metagen(TE=log(OR),seTE=SE,sm="OR")

Egger <- metabias(res, method.bias = "linreg", k.min = 5)

Begg <- metabias(res, method.bias = "rank", k.min = 5)

Trim <- trimfill(res) 

LIST=list(dup_OR, dup_SE, Egger, Begg, Trim)

对不起,我整段文字。

1 个答案:

答案 0 :(得分:0)

这是怎么回事(我已经自由生成了一个最小的工作示例):

## define a function that appends something to an existing list
appendtolist = function(oldlist, add_element){

    if(class(add_element) == "list"){
        oldlist = c(oldlist, add_element)
    }
    else if(class(add_element) != "list"){
        oldlist[[length(oldlist) + 1]] = add_element
    }
    return(oldlist)
}

## define a test list
firstlist = list("a", c(1:10), "test")

## add content to the first list
newlist = appendtolist(firstlist, c(1:1000))