[R] How to compare rows of two matrices

Stephen C. Upton supton at referentia.com
Thu Aug 24 12:26:31 CEST 2006


Does this work for you?
dd <- mapply("==",train,test)
 > dim(dd) <- dim(train)
 > dd
      [,1]  [,2]  [,3]  [,4]
[1,] FALSE FALSE FALSE FALSE
[2,]  TRUE FALSE FALSE  TRUE
[3,] FALSE FALSE FALSE FALSE
[4,] FALSE FALSE FALSE FALSE
[5,] FALSE FALSE FALSE  TRUE

HTH
steve


Muhammad Subianto wrote:
> Dear all,
> I have a dataset
> train <- cbind(c(0,2,2,1,0), c(8,9,4,0,2), 6:10, c(-1, 1, 1, -1, 1))
> test <- cbind(1:5, c(0,1,5,1,3), c(1,1,2,0,3) ,c(1, 1, -1, 1, 1))
>
> I want to find which rows of train and test it different in its last
> column (column 4).
> The solution must be something like
>
> train
>      [,1] [,2] [,3] [,4]
> [1,]    0    8    6   -1
> [3,]    2    4    8    1
> [4,]    1    0    9   -1
>
>
> test
>      [,1] [,2] [,3] [,4]
> [1,]    1    0    1    1
> [3,]    3    5    2   -1
> [4,]    4    1    0    1
>
> I have tried with
> matrix(train %in% test, dim(train))
> apply(train, 1, paste, collapse="") %in% apply(test, 1, paste, collapse="")
>
> It doesn't work.
> How can I do.
> Thanks for any help.
>
> Best, Muhammad Subianto
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
>
>



More information about the R-help mailing list