根据Venn图中R中另一列中的字符串计算列中的所有值 [英] Count all values in a column based on string in another column in R for a Venn diagram

查看:121
本文介绍了根据Venn图中R中另一列中的字符串计算列中的所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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 ,D,A,B_D,A_B_D),
V2 = c(15057,5,9,10,94,1250,3943,11,2517,5,5,2280 ,5,1735,4))

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

 图书馆(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 =
cex = 2,
cat.cex = 2,
cat.col = c(orange,red,green,blue)
);

在这种情况下,我需要从D $ V2中计算所有值为A在列V1等。那么我将需要适当地订购venn plot函数。

解决方案

这是我将要做的

 #setup 
myset = LETTERS [1:4]

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

#构造计数
myn < - length(myset)
mynums < - unlist(sapply(seq(myn)),function(n)
apply (D,sum(V2 [Reduce(&,mget(...)),((n == myn)matrix(seq(myn))else combn(myn,n) myset [x])))))
)))

#通过计数器
do.call(draw.quad.venn,c(as.list(unname (mynums)),列表(
category = myset,
fill = c(orange,red,green,blue),
lty =dotted
cex = 2,
cat.cex = 2,
cat.col = c(orange,red,green,blue)
) )


I have a file that I converted to a dataframe that looks as follows:

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))

I need to convert this dataframe into a list of numbers that I can use to create a 4-way venn plot. In this example the values are the correct values if added correctly. I did this manually but since I need to create several similar plots I would like to find a way to do this more efficiently.

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")
);

In this case I would need to count up all values from D$V2 that has an "A" in column V1 and so on. Then I would need to order appropriately for the venn plot function.

解决方案

Here's what I would do

# 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")
)))

这篇关于根据Venn图中R中另一列中的字符串计算列中的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆