[英]Is it possible to ask R randomly pick two variables from a group of binary variables to calculate the proportion?
我在數據集中有幾個二元變量,我想計算兩個變量的所有組合的“1 1”的比例。例如(a1=1 和 a2=1)的比例。 我可以每次手動指定兩個變量來運行代碼,但是我的數據中有 10 個以上的變量,因此至少會有 45 種組合。 有沒有辦法讓 R 自動配對變量來為我計算?
structure(list(a1 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L),
.Label = c("0", "1"), class = "factor"),
a2 = structure(c(1L,1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L),
.Label = c("0", "1"), class = "factor"),
a3 = structure(c(1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L,1L, 1L),
.Label = c("0", "1"), class = "factor"),
a4 = structure(c(1L,2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L),
.Label = c("0","1"), class = "factor"),
a5 = structure(c(2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L),
.Label = c("0", "1"), class = "factor")), row.names = 180:190, class = "data.frame")
我們可以創建一個函數來獲取選擇兩個列名的sample
,然后對數據進行子集,檢查兩列是否都等於 1,得到mean
f1 <- function(dat) {
nm1 <- sample(names(dat), 2, replace = FALSE)
setNames(mean(dat[,nm1[1]]== 1 & dat[,nm1[2]] == 1), paste(nm1, collapse="_"))
}
f1(df1)
# a3_a5
# 0
如果我們想要所有的組合
f1 <- function(dat) {
combn(names(dat), 2, FUN = function(nm) {
nm1 <- paste(nm, collapse="_")
setNames(mean(dat[, nm[1]] ==1 & dat[, nm[2]] == 1), nm1)},
simplify = FALSE)
}
f1(df1)
#[[1]]
#a1_a2
# 0
#[[2]]
#a1_a3
# 0
#[[3]]
#a1_a4
# 0
#[[4]]
# a1_a5
#0.09090909
#[[5]]
# a2_a3
#0.09090909
#[[6]]
#a2_a4
# 0
#[[7]]
# a2_a5
#0.09090909
#[[8]]
#a3_a4
# 0
#[[9]]
#a3_a5
# 0
#[[10]]
#a4_a5
# 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.