我有一个 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")))