R 在数据帧上重复一个函数并存储输出

R 在数据帧上重复一个函数并存储输出,r,simulation,repeat,R,Simulation,Repeat,我模拟了一个包含200行x 1000列的数据矩阵。它包含二项分布中的0和1。1发生的概率取决于我创建的概率矩阵 然后我转置这个数据矩阵并将其转换为数据帧。我创建了一个函数,将丢失的数据引入数据框的每一行。引入缺失数据后,该函数还将向数据框中添加三列。一列是1000行中每行的计算频率1。第二列是每行0的计算频率。第三列是每行缺失值的频率 我想用相同的输入数据帧(没有缺失值的数据帧)重复此函数500次,并输出三个数据帧:一个数据帧有500列,包含0的所有计算频率(每个模拟一列),另一个数据帧有500

我模拟了一个包含200行x 1000列的数据矩阵。它包含二项分布中的0和1。1发生的概率取决于我创建的概率矩阵

然后我转置这个数据矩阵并将其转换为数据帧。我创建了一个函数,将丢失的数据引入数据框的每一行。引入缺失数据后,该函数还将向数据框中添加三列。一列是1000行中每行的计算频率1。第二列是每行0的计算频率。第三列是每行缺失值的频率

我想用相同的输入数据帧(没有缺失值的数据帧)重复此函数500次,并输出三个数据帧:一个数据帧有500列,包含0的所有计算频率(每个模拟一列),另一个数据帧有500列,包含1的所有计算频率,还有一个有500列缺失的数据频率

我已经看到
mapply()
用于类似的东西,但不确定它是否适用于我的情况。如何对数据帧重复应用函数,并在每次重复该函数时存储该函数中执行的每次计算的输出

谢谢大家!

    ####Load Functions####
    ###Compute freq of 0's
    compute.al0 = function(GEcols){
      (sum(GEcols==0, na.rm=TRUE)/sum(!is.na(GEcols))) 
    }

    ###Compute freq of 1's
    compute.al1 = function(GEcols){
      (sum(GEcols==1, na.rm=TRUE)/sum(!is.na(GEcols)))
    }

    #Introduce missing data
    addmissing = function(GEcols){
      newdata = GEcols
      num.cols = 200
      num.miss = 10
      set.to.missing = sample(num.cols, num.miss, replace=FALSE) #select num.miss to be set to missing
      newdata[set.to.missing] = NA
      return(newdata) #why is the matrix getting transposed during this??
    }

    #Introduce missing data and re-compute freq of 0's and 1's, and missing data freq
    rep.missing = function(GEcols){
      indata = GEcols
      missdata = apply(indata,1,addmissing)
      missdata.out = as.data.frame(missdata) #have to get the df back in the right format
      missdata.out.t = t(missdata.out)
      missdata.new = as.data.frame(missdata.out.t)
      missdata.new$allele.0 = apply(missdata.new[,1:200], 1, compute.al0) #compute freq of 0's
      missdata.new$allele.1 = apply(missdata.new[,1:200], 1, compute.al1) #compute freq of 1's
      missdata.new$miss = apply(missdata.new[,1:200], 1, function(x) {(sum(is.na(x)))/200}) #compute missing
      return(missdata.new)  
    }


    #Generate a data matrix with no missing values
    datasim = matrix(0, nrow=200, ncol=1000) #pre-allocated matrix of 0's of desired size
    probmatrix = col(datasim)/1000 #probability matrix, each of the 1000 columns will have a different prob
    datasim2 = matrix(rbinom(200 * 1000,1,probmatrix), 
              nrow=200, ncol=1000, byrow=FALSE) #new matrix of 0's and 1's based on probabilities

    #Assign column names
    cnum = 1:1000
    cnum = paste("M",cnum,sep='')
    colnames(datasim2) = cnum
    #Assign row names
    rnum = 1:200
    rnum = paste("L",rnum,sep='')
    rownames(datasim2) = rnum

    datasim2 = t(datasim2) #data will be used in the transposed form
    datasim2 = as.data.frame(datasim2)

    #add 10 missing values per row and compute new frequencies
    datasim.miss = rep.missing(datasim2)

    #Now, how can I repeat the rep.missing function 
    #500 times and store the output of the new frequencies 
    #generated from each repetition?

我不知道你不知道该怎么做。 如果您不知道如何重复存储结果。一种方法是使用一个全局变量,在函数中执行更新:

弗兰克,谢谢你的
replicate()
建议。我可以通过在
rep.missing()
函数中将
return(missdata.new)
更改为
return(list(missdata.new))
来返回重复。然后我用replicate(500,rep.missing(datasim2),simplify=“matrix”)调用函数

这几乎正是我想要的。我想做什么

    return(list(missdata.new$allele.0, missdata.new$allele.1, missdata.new$miss))
rep.missing()
中,将这3个向量作为列表中的3个列绑定数据帧返回。一个数据框包含500次重复的
错误数据。新的$allege.0
,一个数据框包含500次重复的
错误数据。新的$allege.1
,等等

    replicate(500, rep.missing(datasim2), simplify="matrix")
    replicate(500, rep.missing(datasim2), simplify="matrix")