1

我有一个 csv 文件,第一行是纯文本,其他行是数值。我需要将第一行读入字符向量。

matrixR = read.csv("testR.csv",sep=",",header=FALSE)
matrixR[1,]
    V1     V2      V3    V4  V5  
academic adagio admiral allan alt

以上是第一行。除了这个文件,我还有另一个与第一个文件格式相同的 csv 文件。我是这样读的:

matrixJ = read.csv("testJ.csv",sep=",",header=FALSE)
matrixJ[1,]
    V1     V2      V3    V4  V5  
academic  admiral abc alt bd

对于matrixR第一行中的每个条目,我需要检查它是否也存在于matrixJ的第一行中。如果存在,我需要在matrixJ的第一行捕获它的对应位置。我就是这样做的

which(matrixR[1,2]==matrixJ[1,])
Error in matrixR[1, 2] == matrixJ[1, ] : 
  comparison of these types is not implemented
In addition: Warning message:
  In which(matrixR[1, 2] == matrixJ[1, ]) :
  Incompatible methods ("Ops.factor", "Ops.data.frame") for "=="

实现这种功能的正确方法应该是什么?谢谢。

以下是我按照建议所做的,看起来结果仍然不正确。

> matrixR = as.matrix(read.csv("testWeightR1.csv",sep=",",header=FALSE))
> matrixJ =as.matrix(read.csv("testWeightJ1.csv",sep=",",header=FALSE))
> which(unlist(matrixR[1,]) %in% unlist(matrixJ[1,]))
   [1] 1 2 3 4 6

matrixR 和 matrixJ 的信息如下:

matrixR[1,] V1 V2 V3 V4 V5 V6 "academic" "adagio" "admiral" "allan" "test" "admiral" matrixJ[1,] V1 V2 V3 V4 V5 V6 "academic" "adagio" "admiral" "艾伦“alt”“ans”dput(head(matrixR))

 structure(c("academic", "0", "0", "0", "0", "0", "adagio", "0", 
 "0", "0", "0", "0", "admiral", "0", "0", "0", "0", "0", "allan", 
 "0", "0", "0", "0", "0", "test", "2", "2", "1", "1", "1", "admiral", 
 "0", "0", "0", "0", "0"), .Dim = c(6L, 6L), .Dimnames = list(
  NULL, c("V1", "V2", "V3", "V4", "V5", "V6")))
 > dput(head(matrixJ))
 structure(c("academic", "0", "0", "0", "0", "0", "adagio", "0", 
 "0", "0", "0", "0", "admiral", "0", "0", "0", "0", "0", "allan", 
 "0", "0", "0", "0", "0", "alt", "2", "2", "1", "1", "1", "ans", 
 "0", "1", "1", "0", "1"), .Dim = c(6L, 6L), .Dimnames = list(
  NULL, c("V1", "V2", "V3", "V4", "V5", "V6")))
4

1 回答 1

0

这应该给你你想要使用%in% with哪个`:

which(matrixR[1,] %in% matrixJ[1,])

==您与一个项目进行比较时。当您与矢量使用进行比较时%in%

编辑...

如果是矩阵,请尝试:

which(unlist(matrixR[1,]) %in% unlist(matrixJ[1,]))

编辑 2...

现在您已经提供了可重现的示例,可以更轻松地为您提供更准确的建议:

matrixR  <-
 structure(c("academic", "0", "0", "0", "0", "0", "adagio", "0", 
 "0", "0", "0", "0", "admiral", "0", "0", "0", "0", "0", "allan", 
 "0", "0", "0", "0", "0", "test", "2", "2", "1", "1", "1", "admiral", 
 "0", "0", "0", "0", "0"), .Dim = c(6L, 6L), .Dimnames = list(
  NULL, c("V1", "V2", "V3", "V4", "V5", "V6")))

matrixJ <-
 structure(c("academic", "0", "0", "0", "0", "0", "adagio", "0", 
 "0", "0", "0", "0", "admiral", "0", "0", "0", "0", "0", "allan", 
 "0", "0", "0", "0", "0", "alt", "2", "2", "1", "1", "1", "ans", 
 "0", "1", "1", "0", "1"), .Dim = c(6L, 6L), .Dimnames = list(
  NULL, c("V1", "V2", "V3", "V4", "V5", "V6")))

#> matrixR 
#     V1         V2       V3        V4      V5     V6       
#[1,] "academic" "adagio" "admiral" "allan" "test" "admiral"
#[2,] "0"        "0"      "0"       "0"     "2"    "0"      
#[3,] "0"        "0"      "0"       "0"     "2"    "0"      
#[4,] "0"        "0"      "0"       "0"     "1"    "0"      
#[5,] "0"        "0"      "0"       "0"     "1"    "0"      
#[6,] "0"        "0"      "0"       "0"     "1"    "0"      
#> 
#> matrixJ 
#     V1         V2       V3        V4      V5    V6   
#[1,] "academic" "adagio" "admiral" "allan" "alt" "ans"
#[2,] "0"        "0"      "0"       "0"     "2"   "0"  
#[3,] "0"        "0"      "0"       "0"     "2"   "1"  
#[4,] "0"        "0"      "0"       "0"     "1"   "1"  
#[5,] "0"        "0"      "0"       "0"     "1"   "0"  
#[6,] "0"        "0"      "0"       "0"     "1"   "1"  

which(matrixR[1, ] %in% matrixJ[1, ])

#> which(matrixR[1, ] %in% matrixJ[1, ])
#[1] 1 2 3 4 6

请注意,这是我最初给您的回复,除非您正在寻找其他东西:

于 2012-08-06T14:09:59.993 回答