2

我正在处理 IP 地址数据,并将数据作为字符串读取。为简单起见,我想出一个简单的例子。我试图将矩阵“m”的字符串行与矩阵“abc”中的行匹配。但我一直未能获得正确的输出。

> abc <- matrix(c("dog", "king", "bee", "cat", "apple","eel", "crab", "ball","soap","crazy"), nrow = 5)
> abc
     [,1]    [,2]   
[1,] "dog"   "eel"  
[2,] "king"  "crab" 
[3,] "bee"   "ball" 
[4,] "cat"   "soap" 
[5,] "apple" "crazy"

> m <- matrix(c("dog", "bee","eel","ball" ), nrow = 2)
> m
     [,1]  [,2]  
[1,] "dog" "eel" 
[2,] "bee" "ball"

我得到输出,这没有任何意义:

> match(data.frame(t(abc)), data.frame(t(m)))
[1] 1 2 2 1 1

我期望输出为 [1] 1 NA 2 NA NA

4

1 回答 1

3

这是一个factor问题,试试这个:

match(data.frame(t(abc), stringsAsFactors = F),
      data.frame(t(m), stringsAsFactors = F))
#[1]  1 NA  2 NA NA
于 2013-06-19T20:41:03.543 回答