[英]r generate a column with random 1s and 0s with restrictions
我有一個包含 500 個觀察值的數據集。 我喜歡根據兩種情況隨機生成 1 和 0
當前數據集
Id Age Category
1 23 1
2 24 1
3 21 2
. . .
. . .
. . .
500 27 3
方案 1
方案 2
預計 Output
Id Age Category Indicator
1 23 1 1
2 24 1 0
3 21 2 1
. . .
. . .
. . .
500 27 3 1
我知道 function sample(c(0,1), 500)
會生成 1s,但我不知道如何讓它隨機生成 200 個 1s。 也不知道如何在 Category1 中隨機生成 80 個 1,在 Category2 中隨機生成 80 個 1,在 Category3 中生成 40 個 1。
這是一個完整的工作示例。
假設您的數據如下所示:
set.seed(69)
df <- data.frame(id = 1:500,
Age = 20 + sample(10, 500, TRUE),
Category = sample(3, 500, TRUE))
head(df)
#> id Age Category
#> 1 1 21 2
#> 2 2 22 2
#> 3 3 28 3
#> 4 4 27 2
#> 5 5 27 1
#> 6 6 26 2
現在,您沒有提到每個類別有多少,所以讓我們檢查一下我們的樣本中有多少:
table(df$Category)
#> 1 2 3
#> 153 179 168
場景 1 很簡單。 您需要創建一個包含 500 個零的向量,然后將一個 1 寫入新向量的 200 個索引樣本中:
df$label <- numeric(nrow(df))
df$label[sample(nrow(df), 200)] <- 1
head(df)
#> id Age Category label
#> 1 1 21 2 1
#> 2 2 22 2 1
#> 3 3 28 3 0
#> 4 4 27 2 0
#> 5 5 27 1 0
#> 6 6 26 2 1
所以我們有隨機的零和一,但是當我們計算它們時,我們有:
table(df$label)
#>
#> 0 1
#> 300 200
場景 2 類似,但涉及更多一點,因為我們需要按類別分組執行類似的操作:
df$label <- numeric(nrow(df))
df <- do.call("rbind", lapply(split(df, df$Category), function(d) {
n_ones <- round(nrow(d) * 0.4 / ((d$Category[1] %/% 3) + 1))
d$label[sample(nrow(d), n_ones)] <- 1
d
}))
head(df)
#> id Age Category label
#> 1.5 5 27 1 0
#> 1.10 10 24 1 0
#> 1.13 13 23 1 1
#> 1.19 19 24 1 0
#> 1.26 26 22 1 1
#> 1.27 27 24 1 1
現在,由於每個類別中的數字不能很好地被 10 整除,我們無法准確地得到 40% 和 20%(盡管您可能使用自己的數據),但我們會盡可能接近它,如下所示:
label_table <- table(df$Category, df$label)
label_table
#> 0 1
#> 1 92 61
#> 2 107 72
#> 3 134 34
apply(label_table, 1, function(x) x[2]/sum(x))
#> 1 2 3
#> 0.3986928 0.4022346 0.2023810
由reprex package (v0.3.0) 於 2020 年 8 月 12 日創建
另一種填充隨機值的方法是創建一個可能值向量(80 個值為 1,nrow-80 個值為 0),然后從這些可能值中采樣。 與通過索引設置值相比,這可以使用更多的 memory,但是潛在值的向量非常小,通常是微不足道的。
set.seed(42)
df <- data.frame(id = 1:500,
Age = 20 + sample(10, 500, TRUE),
Category = sample(3, 500, TRUE))
## In Tidyverse
library(tidyverse)
set.seed(42)
df2 <- df %>%
group_by(Category) %>%
mutate(Label = case_when(
Category == 1 ~ sample(
c(rep(1,80),rep(0,n()-80)),
n()
),
Category == 2 ~ sample(
c(rep(1,80),rep(0,n()-80)),
n()
),
Category == 3 ~ sample(
c(rep(1,40),rep(0,n()-40)),
n()
)
))
table(df2$Category,df2$Label)
# 0 1
# 1 93 80
# 2 82 80
# 3 125 40
## In base
df3 <- df
df3[df$Category == 1,"Label"] <- sample(
c(rep(1,80),rep(0,nrow(df[df$Category == 1,])-80)),
nrow(df[df$Category == 1,])
)
df3[df$Category == 2,"Label"] <- sample(
c(rep(1,80),rep(0,nrow(df[df$Category == 2,])-80)),
nrow(df[df$Category == 2,])
)
df3[df$Category == 3,"Label"] <- sample(
c(rep(1,40),rep(0,nrow(df[df$Category == 3,])-40)),
nrow(df[df$Category == 3,])
)
table(df3$Category,df3$Label)
# 0 1
# 1 93 80
# 2 82 80
# 3 125 40
要解決方案 1,您需要創建一個包含 300 個零和 200 個 1 的向量,然后與該向量相同而無需替換。
pull_from = c(rep(0,300), rep(1,200))
sample(pull_from, replace = FALSE)
對於場景 2,我建議根據類別將您的數據分成 3 個單獨的塊,重復上述步驟,為您需要的零和零的數量使用不同的值,然后重新組合成一個 dataframe。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.