繁体   English   中英

比较 R 中的两个字符向量

[英]Compare two character vectors in R

我有两个 ID 的字符向量。

我想比较两个字符向量,特别是我对以下数字感兴趣:

  • A 和 B 中都有多少个 ID
  • A中有多少个ID但B中没有
  • B中有多少个ID但A中没有

我也很想画一个维恩图。

以下是一些可以尝试的基础知识:

> A = c("Dog", "Cat", "Mouse")
> B = c("Tiger","Lion","Cat")
> A %in% B
[1] FALSE  TRUE FALSE
> intersect(A,B)
[1] "Cat"
> setdiff(A,B)
[1] "Dog"   "Mouse"
> setdiff(B,A)
[1] "Tiger" "Lion" 

同样,您可以简单地获得计数:

> length(intersect(A,B))
[1] 1
> length(setdiff(A,B))
[1] 2
> length(setdiff(B,A))
[1] 2

我通常处理大型集,所以我使用表格而不是维恩图:

xtab_set <- function(A,B){
    both    <-  union(A,B)
    inA     <-  both %in% A
    inB     <-  both %in% B
    return(table(inA,inB))
}

set.seed(1)
A <- sample(letters[1:20],10,replace=TRUE)
B <- sample(letters[1:20],10,replace=TRUE)
xtab_set(A,B)

#        inB
# inA     FALSE TRUE
#   FALSE     0    5
#   TRUE      6    3

还有另一种方式,使用%in%和公共元素的布尔向量而不是intersectsetdiff 我认为您实际上想要比较两个向量,而不是两个列表-列表是一个 R 类,可能包含任何类型的元素,而向量始终只包含一种类型的元素,因此更容易比较真正相等的元素。 在这里,元素被转换为字符串,因为这是目前最不灵活的元素类型。

first <- c(1:3, letters[1:6], "foo", "bar")
second <- c(2:4, letters[5:8], "bar", "asd")

both <- first[first %in% second] # in both, same as call: intersect(first, second)
onlyfirst <- first[!first %in% second] # only in 'first', same as: setdiff(first, second)
onlysecond <- second[!second %in% first] # only in 'second', same as: setdiff(second, first)
length(both)
length(onlyfirst)
length(onlysecond)

#> both
#[1] "2"   "3"   "e"   "f"   "bar"
#> onlyfirst
#[1] "1"   "a"   "b"   "c"   "d"   "foo"
#> onlysecond
#[1] "4"   "g"   "h"   "asd"
#> length(both)
#[1] 5
#> length(onlyfirst)
#[1] 6
#> length(onlysecond)
#[1] 4

# If you don't have the 'gplots' package, type: install.packages("gplots")
require("gplots")
venn(list(first.vector = first, second.vector = second))

就像前面提到的,在 R 中绘制维恩图有多种选择。这是使用 gplots 的输出。

带有 gplots 的维恩图

使用 sqldf:较慢但非常适合混合类型的数据帧:

t1 <- as.data.frame(1:10)
t2 <- as.data.frame(5:15)
sqldf1 <- sqldf('SELECT * FROM t1 EXCEPT SELECT * FROM t2') # subset from t1 not in t2 
sqldf2 <- sqldf('SELECT * FROM t2 EXCEPT SELECT * FROM t1') # subset from t2 not in t1 
sqldf3 <- sqldf('SELECT * FROM t1 UNION SELECT * FROM t2') # UNION t1 and t2

sqldf1  X1_10
1
2
3
4
sqldf2   X5_15
11
12
13
14
15
sqldf3   X1_10
1
2 
3 
4 
5 
6 
7
8
9
10
11
12
13      
14
15

使用与上述答案之一相同的示例数据。

A = c("Dog", "Cat", "Mouse")
B = c("Tiger","Lion","Cat")

match(A,B)
[1] NA  3 NA

match函数返回与该位置的矢量B在所有值的A 所以, catA的第二个元素,是B的第三个元素。 没有其他比赛。

要获取AB的匹配值,您可以执行以下操作:

m <- match(A,B)
A[!is.na(m)]
"Cat"
B[m[!is.na(m)]]
"Cat"

要获取AB的不匹配值:

A[is.na(m)]
"Dog"   "Mouse"
B[which(is.na(m))]
"Tiger" "Cat"

此外,您可以使用length()来获取匹配和不匹配值的总数。

如果A是一个 data.table 字段a的类型列表,条目本身作为一个基本类型的向量,例如创建如下

A<-data.table(a=c(list(c("abc","def","123")),list(c("ghi","zyx"))),d=c(9,8))

B是一个带有原始条目向量的列表,例如创建如下

B<-list(c("ghi","zyx"))

并且您正在尝试查找A$a哪个(如果有)元素与B匹配

A[sapply(a,identical,unlist(B))]

如果你只是想在进入a

A[sapply(a,identical,unlist(B)),a]

如果你想的匹配indicies a

A[,which(sapply(a,identical,unlist(B)))]

如果 B 本身是一个与 A 具有相同结构的 data.table,例如

B<-data.table(b=c(list(c("zyx","ghi")),list(c("abc","def",123))),z=c(5,7))

并且您正在寻找一列的两个列表的交集,您需要相同顺序的向量元素。

# give the entry in A for in which A$a matches B$b
A[,`:=`(res=unlist(sapply(list(a),function(x,y){
                                      x %in% unlist(lapply(y,as.vector,mode="character"))
                                  },list(B[,b]),simplify=FALSE)))
  ][res==TRUE
  ][,res:=NULL][] 

# get T/F for each index of A
A[,sapply(list(a),function(x,y){
                      x %in% unlist(lapply(y,as.vector,mode="character"))
                  },list(B[,b]),simplify=FALSE)]

请注意,您不能做一些像

setkey(A,a)
setkey(B,b)
A[B]

加入 A&B 因为你不能在 data.table 1.12.2 中键入list类型的字段

同样,你不能问

A[a==B[,b]]

即使 A 和 B 相同,因为==运算符尚未在 R 中实现用于类型list

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM