双循环以获取R中的数据帧(tidyverse) [英] Double looping to get a dataframe in R (tidyverse)

查看:61
本文介绍了双循环以获取R中的数据帧(tidyverse)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在5个不同的回合中重复执行 sample(1:11,1) 6次。在最终输出中,我想要一个 data.frame ,其中每一轮都是一行(见下文)。

I want to repeat sample(1:11, 1) 6 times at 5 different rounds. In the final output, I want a data.frame where each round is a row (see below).

这在 tidyverse (例如 purrr :: map_df )还是BASE R?

Is this achievable in tidyverse (e.g., purrr::map_df) or 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


推荐答案

我们可以使用复制

t(replicate(5, sample(1:11, 6, replace = TRUE)))

正如@thelatemail提到的,我们可以采样

As @thelatemail mentioned we can sample only once and put the data in a matrix.

nr <- 5
nc <- 6
matrix(sample(1:11, nr * nc, replace = TRUE), nr, nc)

这篇关于双循环以获取R中的数据帧(tidyverse)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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