gpt4 book ai didi

r - 根据 R 中另一列中的字符串对维恩图的一列中的所有值进行计数

转载 作者:行者123 更新时间:2023-12-02 09:07:33 24 4
gpt4 key购买 nike

我有一个已转换为数据框的文件,如下所示:

D <- data.frame(
V1 =c("B", "A_B", "A_B_C", "C_D", "A_C", "C_B_D", "C", "C_A_B_D", "B_C", "C_A_D", "A_D", "D", "A", "B_D", "A_B_D"),
V2 = c(15057, 5, 9, 1090, 4, 1250, 3943, 11, 2517, 5, 5, 2280, 5, 1735, 4))

我需要将此数据帧转换为数字列表,我可以使用它来创建 4 路维恩图。在此示例中,如果添加正确,这些值就是正确的值。我手动完成了此操作,但由于我需要创建几个类似的绘图,因此我想找到一种更有效地完成此操作的方法。

library("VennDiagram")
venn.plot <- draw.quad.venn(
area1 = 48,
area2 = 20588,
area3 = 8829,
area4 = 6380,
n12 = 29,
n13 = 29,
n14 = 25,
n23 = 3787,
n24 = 3000,
n34 = 2356,
n123 = 20,
n124 = 15,
n134 = 16,
n234 = 1261,
n1234 = 11,
category = c("A", "B", "C", "D"),
fill = c("orange", "red", "green", "blue"),
lty = "dashed",
cex = 2,
cat.cex = 2,
cat.col = c("orange", "red", "green", "blue")
);

在这种情况下,我需要计算 D$V2 中 V1 列中有“A”的所有值,依此类推。然后我需要为维恩图函数进行适当的排序。

最佳答案

这就是我要做的

# setup
myset = LETTERS[1:4]

# create dummies
D[,myset] <- lapply(myset, grepl, D$V1)

# construct counts
myn <- length(myset)
mynums <- unlist(sapply(seq(myn), function(n)
apply(if (n==myn) matrix(seq(myn)) else combn(myn,n), 2, function(x)
with(D, sum( V2[Reduce("&", mget(myset[x]))] ))
)))

# pass counts to plotter
do.call(draw.quad.venn, c(as.list(unname(mynums)), list(
category = myset,
fill = c("orange", "red", "green", "blue"),
lty = "dashed",
cex = 2,
cat.cex = 2,
cat.col = c("orange", "red", "green", "blue")
)))

enter image description here

关于r - 根据 R 中另一列中的字符串对维恩图的一列中的所有值进行计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31681110/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com