因此,我编写了一个函数,该函数采用数据帧和唯一数字<1、5> 假设在这种情况下,我们希望唯一数字为3
how_much = 100
A <- sample(how_much, replace = TRUE, x = 1:5)
B <- sample(how_much, replace = TRUE, x = 1:5)
VennData <- data.frame(A, B)
,然后返回如下所示的表:
count A B
24 TRUE TRUE
20 TRUE FALSE
13 FALSE TRUE
43 FALSE FALSE
当我们看到有24个观测值,其中A和B都等于3时, 20个观测值的A等于3,B等于3, 13个观测值的A不等于3并且B等于3等等...
答案 0 :(得分:0)
使用set.seed(43)
library(dplyr)
VennData %>%
mutate(A = (A == 3),
B = (B == 3)) %>%
count(A, B)
## A tibble: 4 x 3
# A B n
# <lgl> <lgl> <int>
#1 FALSE FALSE 64
#2 FALSE TRUE 20
#3 TRUE FALSE 13
#4 TRUE TRUE 3
在基数R中,
aggregate(Count ~ ., transform(VennData, A = A == 3, B = B == 3, Count = 1), sum)
# A B Count
#1 FALSE FALSE 64
#2 TRUE FALSE 13
#3 FALSE TRUE 20
#4 TRUE TRUE 3
答案 1 :(得分:0)
带有data.table
library(data.table)
set.seed(43)
setDT(VennData)[, .N, .(A = A == 3, B = B == 3)]
# A B N
#1: FALSE FALSE 64
#2: FALSE TRUE 20
#3: TRUE TRUE 3
#4: TRUE FALSE 13