如何在dplyr中跨列随机排序 [英] How randomize order across columns in dplyr

查看:46
本文介绍了如何在dplyr中跨列随机排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须创建一个数据集,该数据集将生成带有两个答案选项(是/否)的问卷.我需要将这些选项随机化,将它们写入dataframe,然后将其导出到csv.

I have to create a dataset that will generate a questionnaire with two answer options (yes/no). I need to randomize these options , write them to dataframe, then export it to csv.

所以data.frame看起来像:

So the data.frame looks like:

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 列以将其重新插入数据帧的方式是什么,以是/否的顺序是随机的,每行有一个是和一个否选项?

What is the way in dplyr to generate columns first and second to plug them back to dataframe, in such a way that the order of yes/no is random, and there is one Yes and one No option in each row?

我做类似的事情,但是那当然不起作用:

I do something like that, but it doesn't work of course:

yes_option <-'Yes'
no_option <-'No'
options<-c(yes_option, no_option)
opt_cols <- rep(sample(options, 2),100)

推荐答案

sample 第一列,然后将其余值保留在第二列中.

sample first column and then keep the remaining value in second column.

library(dplyr)

df %>%
  mutate(first = sample(c('yes', 'no'), n(), replace = TRUE), 
         second = ifelse(first == 'yes', 'no', 'yes'))

这篇关于如何在dplyr中跨列随机排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆