重复功能并保存结果。 [R

时间:2017-08-26 18:20:35

标签: r function

有些问题与此类似,但我仍然无法解决我的特定问题。我有一个函数来从df获取行的随机样本。使用dplyr库。

rohis <- function(dat, ind, n) {
    ind <- enquo(ind)
    dat %>%
        filter(UQ(ind) %in% sample(unique(UQ(ind)), n)) %>%
        slice(sample(row_number()))
}

现在我想重复此功能10次并将结果存储在列表中:

lst <- list()
for (i in 1:10){
    i = rohis(data, ind, 3)
    lst[[length(lst) + 1]] = i
}

然而出现了这个错误:

Error in lst[[length(lst) + 1]] <- i : 
object of type 'closure' is not subsettable

以下是一些数据:

data <- structure(list(pos1 = c(5, 15, 25, 40, 80, 5, 18, 22, 38, 84, 
                    5, 16, 50, 92, 31, 50, 20, 30, 50, 70, 27, 50, 60, 50, 90, 20, 
                    40, 54, 65, 78, 7, 17, 27, 42, 85, 10, 19, 25, 39, 88, 10, 19, 
                    55, 93, 32, 54, 22, 35, 52, 72, 29, 55, 65, 55, 95, 22, 46, 57, 
                    67, 79), pos2 = c(10, 17, 30, 42, 90, 10, 20, 24, 42, 87, 10, 
                                      19, 52, 100, 40, 70, 25, 32, 60, 90, 30, 60, 71, 60, 100, 25, 
                                      50, 50, 69, 90, 19, 27, 39, 49, 99, 20, 29, 30, 49, 92, 20, 29, 
                                      59, 100, 48, 76, 30, 39, 67, 98, 36, 68, 76, 65, 100, 30, 55, 
                                      55, 70, 100), chr = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 1, 1, 
                                                            1, 1, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 1, 1, 1, 
                                                            1, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 
                                                            2, 2, 2, 2, 2), ind = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                                              1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
                                                                                              3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
                                                                                              4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 
                                                                                              6L, 6L, 6L, 6L, 6L), .Label = c("A", "B", "C", "D", "E", "F"), class ="factor")), .Names = c("pos1", "pos2", "chr", "ind"), row.names = c(NA, -60L), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

<强>解决方案

如果要过滤特定的ind,然后对随机行进行采样,则可以尝试此功能

rohis <- function(dat, I, n) {
    require(dplyr)
    dat %>%
        filter(ind %in% I) %>%
        slice(sample(nrow(.), n))
}

要重复10次并保存在列表中

library(purrr)
ans <- map(1:10, ~rohis(data, "A", 3))

过滤多个ind

ans <- map(1:10, ~rohis(data, c("A","E"), 3))

<强>替代

对未经过滤的随机行进行采样

alt <- function(dat, n) {
    dat %>%
        slice(sample(nrow(.), n))
}

map(1:10, ~alt(data, 3))

采样随机ind

alt2 <- function(dat, n) {
    I <- sample(unique(dat$ind), n)
    dat %>%
        filter(ind %in% I)
}

map(1:10, ~alt2(data, 3))