【发布时间】:2021-04-05 09:56:50
【问题描述】:
我必须创建一个数据集,该数据集将生成带有两个答案选项(是/否)的问卷。我需要随机化这些选项,将它们写入数据框,然后将其导出到 csv。
所以 data.frame 看起来像:
data.frame(msg=rep('Do you agree with this statement?',3),first=c('Yes', 'No', 'Yes'), second=c('No', 'Yes', 'No') )
输出:
msg first second
1 Do you agree with this statement? Yes No
2 Do you agree with this statement? No Yes
3 Do you agree with this statement? Yes No
dplyr 中生成列first 和second 以将它们插回数据帧的方式是什么,这样是/否的顺序是随机的,并且有一个是和一个否选项每一行?
我做了类似的事情,但它当然不起作用:
yes_option <-'Yes'
no_option <-'No'
options<-c(yes_option, no_option)
opt_cols <- rep(sample(options, 2),100)
【问题讨论】: