简体   繁体   English

重采样和合并数据集

[英]Resampling and merge dataset

Consider the following dataset: 考虑以下数据集:

d1 <- c(2, 3, 8)
d2 <- data.frame(d1)
d1 <- c(1, 7, 9, 10)
d3 <- data.frame(d1)

Now I want to randomly draw 3 observations (without replacement) from d3 3 times, and each time I want to merge it with d2 . 现在,我想从d3随机抽取3个观察值(不进行替换)3次,每次我想将其与d2合并。 So I should have three merged data frames with 6 observations. 因此,我应该具有3个合并的数据帧和6个观察值。

I have tried with: 我尝试过:

for (r in 1:3)
{
  sam <- sample(1:4, 3, replace = FALSE)

  merge <- rbind(d2, d3[sam])
}

But this does not work. 但这是行不通的。 Can anyone help me? 谁能帮我?

You can try 你可以试试

library(tidyverse)
d1 <- data.frame(d1=c(2, 3, 8))
d2 <- data.frame(d1=c(1, 7, 9, 10))
1:3 %>% 
  map(~sample_n(d2, 3) %>% 
      bind_cols(d1))
[[1]]
  d1 d11
1  1   2
2  7   3
3  9   8

[[2]]
  d1 d11
1 10   2
2  1   3
3  7   8

[[3]]
  d1 d11
1  9   2
2  7   3
3 10   8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM