【发布时间】:2021-08-17 17:40:44
【问题描述】:
我有两个相同的数据集,除了一个变量。例如,如下所示,我有两个名为 boys_miss 和 boys_miss2 的数据集。 boys_miss2 有一个额外的二进制变量(称为类型),boys_miss 没有。所以我想使用两个数据集中的观察变量来确定boys_miss 中的type 变量。我不确定这样做的最佳方法是什么。任何解决方案或建议将不胜感激。
# loads relevant packages using the pacman package
pacman::p_load(
mice) # for boys dataset
# set seed
set.seed(2347723)
# generate a samall sample of the boys dataset
boys_miss <- sample(head(boys,100))
# create other dataset that has out variable of interest
boys_miss2 <- boys_miss[sample(1:nrow(boys_miss)), ]
# create the variable of interest
boys_miss2$type <- as.factor(sample(c("runner", "swimmer"),
size = nrow(boys_miss2),
replace = TRUE,
prob = c(.76, .24)))
# Goal here is to replicate type variable in `boys_miss` dataset using the values the matching
# in `boys_miss` and `boys_miss2`
【问题讨论】:
标签: r database dplyr data-wrangling