[R] How to compare rows of two matrices

Petr Pikal petr.pikal at precheza.cz
Thu Aug 24 13:08:35 CEST 2006


Hi

maybe simple math can do it.

different
(train[,4]-test[,4])!=0

same
(train[,4]-test[,4])==0

if you are sure the numbers are integers

HTH
Petr



On 24 Aug 2006 at 12:03, Muhammad Subianto wrote:

Date sent:      	Thu, 24 Aug 2006 12:03:31 +0200
From:           	"Muhammad Subianto" <msubianto at gmail.com>
To:             	r-help at stat.math.ethz.ch
Subject:        	[R] How to compare rows of two matrices

> 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.

Petr Pikal
petr.pikal at precheza.cz



More information about the R-help mailing list