你如何在 R 中制作虚拟数据集?

How do you make a dummy dataset in R?

我如何制作一个数据集,其中每个“站点”、“季节”、“年份”和“物种名称”都完全交叉(每个站点在每个 year/season 期间都被访问过,并且每个物种都可以已在任何时间和地点捕获 -> 即 5 个地点 x 2 个季节 x 2 年 x 2 个物种 = df 大小为 40x5)。

df <- data.frame(site = rep(c("1", "2", "3", "4", "5"), each = 2),
                 season = rep(c("dry", "wet"), each = 10), 
                 year = rep(c(2019, 2020), each = 10), 
                 species_name = rep(c("Sailfin molly", "Hardhead silverside"), each = 10),
                 num = sample(x = 0:15, size  = 20, replace = TRUE))

您可以使用 expand.grid() 函数:

library(tidyverse)

site <- c("1", "2", "3", "4", "5")
season <- c("dry", "wet")
year <-  c(2019, 2020)
species_name <-  c("Sailfin molly", "Hardhead silverside")
num <-  sample(x = 0:15, size  = 40, replace = TRUE)

df <- data.frame(expand.grid(site, season, year, species_name) %>% mutate(num = num))
colnames(df) <- c("site", "season", "year", "species_name", "num")