如何生成有限制的随机数?

问题描述 投票:0回答:1

如何生成随机数并将其限制在一个范围内?

iagegrp
给出年龄界限,
age02
给出点值。我可以在每个范围内生成一个随机变量,但我还想将两个变量之间的年龄差限制为不超过 15。我该怎么做?

dput(data)
        structure(list(iagegrp = structure(c(4, 4, 4, 4, 4, 4, 4, 4, 
        4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3), labels = c(`No answer/refused` = -9, 
        `Don't know` = -8, `Interview not achieved` = -7, `Schedule not applicable` = -2, 
        `Item not applicable` = -1), class = c("haven_labelled", "vctrs_vctr", 
        "double")), age02 = structure(c(72, 66, 34, 37, 50, 38, 62, 49, 
        36, 48, 75, 48, 75, 45, 34, 27, 39, 44, 54, 33), labels = c(`No answer/refused` = -9, 
        `Don't know` = -8, `Interview not achieved` = -7, `Schedule not applicable` = -2, 
        `Item not applicable` = -1), class = c("haven_labelled", "vctrs_vctr", 
        "double"))), row.names = c(NA, 20L), class = "data.frame")


   

iagegrp
,

生成随机数
set.seed(1)    
mutate(age = ifelse(iagegrp == 3, sample(25:44, 7, replace = TRUE), sample(45:64, 13, replace = TRUE))))

如何限制

iagegrp
age02
之间 15 岁的年龄差异。我想要实现的目标的一个例子,

| iagegrp | age02 |
| -------- | -------------- |
| 62    | 72            |
| 64   | 66            |
| 45    | 34            |
| 45   | 37            |
r dplyr
1个回答
0
投票

我不完全确定我收到了你的问题,请随时澄清。但从我得到的情况来看,如果

iagegrp
是 3,那么您需要的年龄在 25 到 44 之间,如果是 4,则需要在 45 到 64 之间。但是您还希望与其他变量
 强制执行最大 15 岁的差异age02
,所以在某些情况下界限会受到更多限制。这是我尝试回答:

library(dplyr)

set.seed(1)  

# Create sample data
iagegrp <- c(4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3)
age02 <- c(72, 66, 34, 37, 50, 38, 62, 49, 36, 48, 75, 48, 75, 45, 34, 27, 39, 44, 54, 33)
df <- data.frame(iagegrp, age02)

df <- df |> 
  # Get bounds given by iagegrp
  mutate(iagegrp_min = ifelse(iagegrp == 3, 25, 45),
         iagegrp_max = ifelse(iagegrp == 3, 44, 64)) |> 
  # Get bounds given by age02
  mutate(age02_min = age02 - 15,
         age02_max = age02 + 15) |> 
  # Get actual bounds
  mutate(age_min = max(iagegrp_min, age02_min),
         age_max = min(iagegrp_max, age02_max)) |> 
  # Sample between bounds
  rowwise() |> # necessary to define custom bounds for each row
  mutate(age = sample(age_min:age_max, size = 1))

它会创建很多中间列,您可以随后使用

df |> select(iagegrp, age02, age)
将其删除。

此外,我无法使用您提供的代码重现数据集,因此我以上面的自定义方式定义了

df

© www.soinside.com 2019 - 2024. All rights reserved.