重新采样和合并数据集

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

请考虑以下数据集:

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

现在我想从d3中随机抽取3次观察(无需替换),每次我想将它与d2合并。所以我应该有三个合并的数据框,有6个观察。

我尝试过:

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

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

但这不起作用。谁能帮我?

r merge resampling
1个回答
0
投票

你可以试试

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
© www.soinside.com 2019 - 2024. All rights reserved.