r - 双循环以获取 R 中的数据帧(tidyverse)
问题描述
我想sample(1:11, 1)
在 5 个不同的回合中重复 6 次。在最终输出中,我想要一个data.frame
每一轮都是一行(见下文)。
这在tidyverse
(例如,purrr::map_df
)或 BASE R 中可以实现吗?
round1 4 5 6 7 8 9
round2 3 2 1 4 4 1
round3 5 4 2 2 1 1
round4 7 7 7 7 7 1
round5 1 8 8 8 8 1
解决方案
我们可以用replicate
t(replicate(5, sample(1:11, 6, replace = TRUE)))
正如@thelatemail 提到的,我们sample
只能将数据放入矩阵中一次。
nr <- 5
nc <- 6
matrix(sample(1:11, nr * nc, replace = TRUE), nr, nc)
推荐阅读
- swift - 为什么没有 http 或 https 的 URL 不能在 webkit 中打开
- stored-procedures - 使用存储过程 postgresql 中的索引从 jsonb[] 中删除特定对象
- php - Twilio:通过 UniqueName 检索房间列表不起作用
- c++ - 编译器(Visual C++)如何优化按索引访问向量元素?
- reactjs - 如何在反应原生的两个视图之间创建一个重叠的圆形图像?
- r - 是否可以在 Visual Code Studio 中打开绘图查看器?
- spring-boot - Spring Cloud Gateway 功能区重试问题
- javascript - 我的正弦波代码的相位控制有什么问题?
- java - 调用 AWS Lambda Java 函数的 AWS 网关收到空正文
- xamarin - 如何从带有参数的 xamarin 表单中的子视图调用父视图中的方法