首页 > 解决方案 > R:从可能值列表中生成数据框

问题描述

newdata <- as_tibble( # valid values shown below
  dvcat="10-24",        # "1-9" "10-24"   "25-39"   "40-54"   "55+"  
  seatbelt="none",      # "none"   "belted"  
  frontal="frontal",    # "notfrontal" "frontal"
  sex="f",              # "f" "m"
  ageOFocc=22,          # age in years, 16-97
  yearVeh=2002,         # year of vehicle, 1955-2003
  airbag="none",        # "none"   "airbag"   
  occRole="pass"        # "driver" "pass"
)
  dvcat seatbelt frontal sex ageOFocc yearVeh airbag occRole
1 10-24     none frontal   f       22    2002   none    pass

我想生成上述变量的可能组合并将它们放入一个 tibble 数据框中。

例如,假设我想要一个包含 3 行的数据集。将随机选择该值以创建新行。

  dvcat seatbelt   frontal sex ageOFocc yearVeh airbag occRole
1 10-24     none   frontal   f       22    2002   none    pass
2 25-39     none   frontal   m       54    2010   none    drive
3 40-54     belted frontal   f       14    2016   airbag    driver

标签: r

解决方案


如果我们有一个list值要选择,那么使用

library(purrr)
map_dfr(lst1, ~ sample(.x, 3, replace = TRUE))
# A tibble: 3 x 8
#  dvcat seatbelt frontal    sex   ageOFocc yearVeh airbag occRole
#  <chr> <chr>    <chr>      <chr>    <int>   <int> <chr>  <chr>  
#1 40-54 none     notfrontal f           71    1997 none   driver 
#2 40-54 none     frontal    m           87    1974 airbag driver 
#3 25-39 belted   notfrontal m           56    2001 none   driver 

或在base R

data.frame(lapply(lst1, sample, size = 3, replace = TRUE))

数据

lst1 <- list(dvcat = c("1-9", "10-24", "25-39", "40-54", "55+"), 
  seatbelt = c("none", 
"belted"), frontal = c("notfrontal", "frontal"), sex = c("f", 
"m"), ageOFocc = 16:97, yearVeh = 1955:2003, airbag = c("none", 
"airbag"), occRole = c("driver", "pass"))

推荐阅读