Skip to Content

regarding the correct approach for comparing two character arrays imported from csv file

I have a csv file, the first row is plain texts, and other rows are numerical values. I need to read this first row into a character vector.

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

The above is the first row. Besides this file, I also have another csv file with the same format as the first file. I read it as follows:

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

For each entry in the first row of matrixR, I need to check whether it also exists in the first row of matrixJ. If it exists, I need to capture its corresponding position in the first row of matrixJ. This is how I did

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 "=="

What should be the correct way to implement this kind of functionality? Thanks.

The following is what I did as suggested, looks like the result is still not correct.

> 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

The information for matrixR and matrixJ is as follows:

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