汇总包含多个字符串的单元格
summarise to sum cells containing multiple strings
我正在尝试将 str_detect 与 sum 结合使用来添加包含字符串集的变量。如何使用下面的 AND 来检测包含 all 字符串的变量而不考虑顺序?
"|"在这种情况下有效,但不适用于“&”
可重现的例子:
data<- data.frame("order" = c("A","B","C","A:B","A:B:C","B:C"))
wrong.out <- data %>% summarise("total"=n(),
"A"= sum(str_detect(order, "A"), na.rm = TRUE),
"B"= sum(str_detect(order, "B"), na.rm = TRUE),
"C"= sum(str_detect(order, "C"), na.rm = TRUE),
"A:B"= sum(str_detect(order, "(A)&(B)"), na.rm = TRUE),
"A:B:C"= sum(str_detect(order, "(A)&(B)&(C)"), na.rm = TRUE))
我正在寻找如下总计,计算同时包含 A&B 或 A&B&C 的任何单元格:
expected.out <- data.frame("total" = "6",
"A"="3",
"B"="4",
"C"="3",
"A:B"="2",
"A:B:C"="1")
对&
条件单独使用str_detect
。
library(dplyr)
library(stringr)
summarise("Total" = n(),
"CD8:PD-1" = sum(str_detect(ordered, "PD-1") &
str_detect(ordered, "CD8"),na.rm = TRUE),
"CD8:PD:1:FoxP3" = sum(str_detect(ordered, "PD-1") &
str_detect(ordered, "CD8") &
str_detect(ordered, "FoxP3"), na.rm = TRUE))
为什么不呢?
data %>% summarise("total"=n(),
"A"= sum(str_detect(order, "A"), na.rm = TRUE),
"B"= sum(str_detect(order, "B"), na.rm = TRUE),
"C"= sum(str_detect(order, "C"), na.rm = TRUE),
"A:B"= sum(str_detect(order, "A:B"), na.rm = TRUE),
"A:B:C"= sum(str_detect(order, "A:B:C"), na.rm = TRUE))
total A B C A:B A:B:C
1 6 3 4 3 2 1
我正在尝试将 str_detect 与 sum 结合使用来添加包含字符串集的变量。如何使用下面的 AND 来检测包含 all 字符串的变量而不考虑顺序?
"|"在这种情况下有效,但不适用于“&”
可重现的例子:
data<- data.frame("order" = c("A","B","C","A:B","A:B:C","B:C"))
wrong.out <- data %>% summarise("total"=n(),
"A"= sum(str_detect(order, "A"), na.rm = TRUE),
"B"= sum(str_detect(order, "B"), na.rm = TRUE),
"C"= sum(str_detect(order, "C"), na.rm = TRUE),
"A:B"= sum(str_detect(order, "(A)&(B)"), na.rm = TRUE),
"A:B:C"= sum(str_detect(order, "(A)&(B)&(C)"), na.rm = TRUE))
我正在寻找如下总计,计算同时包含 A&B 或 A&B&C 的任何单元格:
expected.out <- data.frame("total" = "6",
"A"="3",
"B"="4",
"C"="3",
"A:B"="2",
"A:B:C"="1")
对&
条件单独使用str_detect
。
library(dplyr)
library(stringr)
summarise("Total" = n(),
"CD8:PD-1" = sum(str_detect(ordered, "PD-1") &
str_detect(ordered, "CD8"),na.rm = TRUE),
"CD8:PD:1:FoxP3" = sum(str_detect(ordered, "PD-1") &
str_detect(ordered, "CD8") &
str_detect(ordered, "FoxP3"), na.rm = TRUE))
为什么不呢?
data %>% summarise("total"=n(),
"A"= sum(str_detect(order, "A"), na.rm = TRUE),
"B"= sum(str_detect(order, "B"), na.rm = TRUE),
"C"= sum(str_detect(order, "C"), na.rm = TRUE),
"A:B"= sum(str_detect(order, "A:B"), na.rm = TRUE),
"A:B:C"= sum(str_detect(order, "A:B:C"), na.rm = TRUE))
total A B C A:B A:B:C
1 6 3 4 3 2 1