r - 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
解决方案
如果我们有一个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"))
推荐阅读
- python - KeyError: 'TCL_Library' 当我使用 cx_Freeze 并且 DLL 加载失败
- php - 会话在php中相互重叠和交换
- javascript - Flow 无法解析 pdf 文件
- python - 使用 Python 3 向 MySQL 或 MariaDB 发送请求
- python - 修改规范文件中的二进制文件时,Pyinstaller 无法正常工作
- java - 事务无法解决事务边界 - 休眠
- c# - WPF - 如何在 ViewModel 中实现 IDataErrorInfo 接口到复杂模型(FluentValidation)
- css - 如何从 ReactJS 的内联样式中检索纯 CSS?
- python - dask 调用图:缺少必需的位置参数
- reactjs - Kendo React Grid 编辑模式验证