如何从数据库中采样记录而不重复?

问题描述 投票:0回答:1

下午好,我的问题如下:

我有一个名为friends的数据库:

friends <- data_frame(
  name = c("Nicolas", "Thierry", "Bernard", "Jerome", "peter", "yassine", "karim"),
  age = c(27, 26, 30, 31, 31, 38, 39),
  height = c(180, 178, 190, 185, 187, 160, 158),
  married = c("M", "M", "N", "N", "N", "M", "M")
)

i <- Intervals(
  matrix(
    c(0,5000,  
      0,5000,
      7000,10000,  
      7000,10000,
      7000,10000,
      10000,15000,  
      10000,15000
    ),
    byrow = TRUE,
    ncol = 2
  ),
  closed = c( TRUE, TRUE ),
  type = "R"
) 

我需要创建一个以该数据库为参数的函数。

该函数将对一行进行采样(例如,仅对第四行进行一次采样,该函数将不会选择该行进行其他执行),然后它将执行某些特性。

sampling_fct<-function(data){

data[sample(nrow(data), 1), ]

# sample a given row only one time  

}

如果我们有5行,则选择应类似于:

数据[3]

数据[2]

数据[5]

数据[4]

数据[1]

其中数据=朋友。

我希望我的问题清楚。

感谢您之前!

r
1个回答
0
投票

为了确保仅对给定的行进行一次采样,可以使用sample(replace=FALSE)(re:R Examples of sample())。

给出您的数据集,请考虑使用:

friends <- data.frame(
      name = c("Nicolas", "Thierry", "Bernard", "Jerome", "peter", "yassine", "karim"),
      age = c(27, 26, 30, 31, 31, 38, 39),
      height = c(180, 178, 190, 185, 187, 160, 158),
      married = c("M", "M", "N", "N", "N", "M", "M")
    )

sampling_fct<-function(data){

  data[sample(nrow(data), size = 6, replace = TRUE), ]

}

mylist <- list(friends, friends, friends)

mylist_sampled <- lapply(mylist,sampling_fct)
© www.soinside.com 2019 - 2024. All rights reserved.