从R中的函数中提取命名数据帧

时间:2017-10-01 23:09:55

标签: r function dataframe

我创建了以下需要从中提取一些信息的函数。但是,R给了我一些问题。

HAC.sim <- function(K = 1, N, Hstar, probs, perms = 10000){
specs <- 1:N

### Set up a container to hold the identity of each individual from each permutation

pop <- array(dim = c(c(perms, N), K))

### Create an ID for each haplotype

haps <- as.character(1:Hstar)

### Assign probabilities of occurrence to each haplotype, ensure they sum to 1
### This is where we assume we "know" the distribution of haplotypes
### Here, I have assumed they all occur with equal frequency, but you can change this to            assume some dominant ones and some rare ones, whatever you want

probs <- rep(1/Hstar, Hstar) 
# probs <- c(220/N, rep(3/N, 2), rep(2/N, 2), rep(1/N, 10))

### Generate permutations, we assume each permutation has N individuals, and we sample  those individuals' haplotypes from our probabilities

# If K > 1, haplotypes are partitioned into equally-sized subpopulations/demes
# Can change number of haplotypes in each subpopulation and re-run simulation 
# For each additional, K, add new Ki and new pop[j ,, i] in loop

for(j in 1:perms){
    for(i in 1:K){ 
        if(i == 1){
            pop[j, specs, i] <- sample(haps, size = N, replace = TRUE, prob = probs)
        }
            else{
                pop[j ,, 1] <- sample(haps[K1], size = N, replace = TRUE, prob = probs[K1])
                pop[j ,, 2] <- sample(haps[K2], size = N, replace = TRUE, prob = probs[K2]) 
        }
    }
}

### Make a matrix to hold the 1:N individuals from each permutation

HAC.mat <- array(dim = c(c(perms, N), K))

for(k in specs){
    for(j in 1:perms){
        for(i in 1:K){ 
            ind.index <- sample(specs, size = k, replace = FALSE) ## which individuals will we sample
            hap.plot <- pop[sample(1:nrow(pop), size = 1, replace = TRUE), ind.index, sample(1:K, size          = 1, replace = TRUE)] ## pull those individuals from a permutation
            HAC.mat[j, k, i] <- length(unique(hap.plot))  ## how many haplotypes did we get for a given sampling intensity (k) from each ### permutation (j)
    }
  }
}

### Calculate the mean and CI for number of haplotypes at each sampling intensity (j)

means <- apply(HAC.mat, MARGIN = 2, mean)
lower <- apply(HAC.mat, MARGIN = 2, function(x) quantile(x, 0.025))
upper <- apply(HAC.mat, MARGIN = 2, function(x) quantile(x, 0.975))

### Plot the curve and frequency barplot

par(mfrow = c(1, 2))

for(i in 1:K){
    if(i == 1){
        plot(specs, means, type = "n", xlab = "Specimens sampled", ylab = "Unique haplotypes",  ylim = c(1, Hstar))
        polygon(x = c(specs, rev(specs)), y = c(lower, rev(upper)), col = "gray")
        lines(specs, means, lwd = 2)
        HAC.bar <- barplot(N*probs, xlab = "Unique haplotypes", ylab = "Specimens sampled", names.arg = 1:Hstar)
    }
    else{
        plot(specs, means, type = "n", xlab = "Specimens sampled", ylab = "Unique haplotypes", ylim = c(1, max(HAC.mat)))
        polygon(x = c(specs, rev(specs)), y = c(lower, rev(upper)), col = "gray")
        lines(specs, means, lwd = 2)
        HAC.bar <- barplot(N*probs[get(paste0("K", i))], xlab = "Unique haplotypes", ylab = "Specimens sampled", names.arg = get(paste0("K",i)))
  }
}

d <- data.frame(specs, means)

## Measures of Closeness ##

list(c(cat("\n Number of haplotypes sampled: " , max(means)) 

cat("\n Number of haplotypes not sampled: " , Hstar - max(means))

cat("\n Proportion of haplotypes sampled: " ,  max(means)/Hstar)

cat("\n Proportion of haplotypes not sampled: " , (Hstar - max(means))/Hstar)), d)

}

HAC.sim(K = 1, N = 100, Hstar = 10)

我希望能够轻松提取数据框&#39; d&#39;从上面的功能。

我需要将d传递给回归模型对象。

输出几乎给了我所需要的东西,除了在[Hstar - max(均值))/ Hstar的值的末尾附加[[1]],而不是在新行上。另外,[[1]]是一个NULL列表对象。为什么我将这个NULL列表作为输出,我该如何解决这个问题呢?

感谢任何帮助。

0 个答案:

没有答案