我可以制作一个函数来使用循环制作这样的数据框吗?(后续问题) [英] Can I make a function that makes a dataframe like this using loops? (follow up question)
问题描述
感谢您对此问题的关注.
Thank you for your interest in this question.
我有如下数据.
a<- data.frame("Grade"=c(1, 2, 3, 4), "Prob"=c(0.01, 0.25, 0.45, 0.29))
b<- data.frame("Pot"= c(letters[1:18]))
基于下面的代码,我想制作一个函数,该函数可以根据概率(replace=TRUE)和四个相同概率的随机字母(replace=FALSE)循环 4 个成绩数字.例如,这个循环可能如下所示:
Based on the codes below, I'd like to make a function that can loop 4 Grade numbers based on the Prob probability (replace=TRUE) and four random letters with the same probability (replace=FALSE). For instance, this loop may look like below:
3 2 3 2 d f k g
1 3 4 2 a k r b
我想做一个函数,它不仅可以计算成绩结果仅低于 3 的结果,以及我选择的四个字母出现的结果,还可以计算获得此结果的试验次数.所以,如果我想让 Pot 有a"、b"、c"和d"结果将如下所示:
I'd like to make a function that can compute not only the results in which the Grades result is only lower than 3, and the four alphabets that I selected appear, but the number of trials to get this result. So, if I want Pot to have "a", "b", "c", and "d" the result will look like:
Trial Grade Pot
15 3 2 1 3 a b c d
39 2 1 2 2 d b a c
2 3 3 3 3 d a b d
77 3 2 3 3 c d b a
感谢一位非常友善的人,我可以学习以下代码,但我无法对其进行编辑以获得我希望看到的结果.你能帮我吗?
I could learn the below code thanks to a very kind person, but I can't edit it to get the results I hope to see. Can you please help me?
samplefun <- function(a) {
c <- sample(a$Grade, size=4, prob=a$Prob, replace=TRUE)
res <- tibble(
Trial = which(c < 3)[1],
Result = c[which(c < 3)[1]]
)
nsamples <- 1000
x<-map_dfr(1:nsamples, ~ samplefun(a))
感谢您阅读这个问题.
推荐答案
这是我认为您所追求的解决方案.我在采样 b$Pot
时没有指定概率向量,因为你没有在你的问题中给出一个长度为 18 个元素的概率向量(见我的评论).
Here's a solution to what I think you're after. I haven't specified a probability vector when sampling b$Pot
, because you didn't give one that was 18 elements long in your question (see my comment).
library(tidyverse)
a<- data.frame(Grade =c(1, 2, 3, 4), Prob = c(0.01, 0.25, 0.45, 0.29))
b<- data.frame(Pot = letters[1:18])
chosenletters <- c("a", "b", "c", "d")
samplefun <- function(a, b, chosenletters) {
ntrials <- 0
repeat {
grades <- sample(a$Grade, size = 4, prob = a$Prob, replace = T)
chars <- sample(b$Pot, size = 4, replace = F)
ntrials <- ntrials + 1
if (all(grades < 4) & all(chars %in% chosenletters)) {break}
}
return( tibble(Trial = ntrials, Grade = list(grades), Letters = list(chars)) )
}
nsamples <- 5
res <- map_dfr(1:nsamples, ~ samplefun(a, b, chosenletters))
这个数据框 res
给出了嵌入在每个数据框单元格内的列表中的正确等级和字母,以及生成结果的试验.
This dataframe res
gives the correct Grades and Letters embedded in lists inside each dataframe cell, plus the trial at which the result was generated.
# A tibble: 5 x 3
Trial Grade Letters
<dbl> <list> <list>
1 20863 <dbl [4]> <fct [4]>
2 8755 <dbl [4]> <fct [4]>
3 15129 <dbl [4]> <fct [4]>
4 1033 <dbl [4]> <fct [4]>
5 5264 <dbl [4]> <fct [4]>
更好地查看嵌套列表:
> glimpse(res)
Rows: 5
Columns: 3
$ Trial <dbl> 20863, 8755, 15129, 1033, 5264
$ Grade <list> <3, 3, 3, 3>, <3, 2, 2, 2>, <3, 3, 2, 2>, <3, 3, 2, 3>, <3, 2, 3, 3>
$ Letters <list> <b, a, c, d>, <b, a, c, d>, <c, a, b, d>, <b, d, c, a>, <a, b, d, c>
这篇关于我可以制作一个函数来使用循环制作这样的数据框吗?(后续问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!