[R] Finding all rows of a matrix equal to vector
Denis Kazakiewicz
d.kazakiewicz at gmail.com
Sat Jul 16 11:58:39 CEST 2011
Hi
a bit ugly approach
but it works
x1<-c(1,2,3)
x2<-c(1,5,6)
x3<-c(7,8,9)
A<-matrix(c(rep(x1,5),rep(x2,5),rep(x3,5),rep(x1,5)),nrow=20,ncol=3,byrow=T)
A
B <- apply(A,1, FUN = function(x)paste(x, collapse = ''))
table(B)
On 16.07.2011 12:29, Sebastian Lerch wrote:
> Hi everyone,
>
> my question might be very trivial, but I could not come up with an
> answer...
>
> I want to find out how often a matrix contains a certain vector as row:
>
> x1<-c(1,2,3)
> x2<-c(1,5,6)
> x3<-c(7,8,9)
> A<-matrix(c(rep(x1,5),rep(x2,5),rep(x3,5),rep(x1,5)),nrow=20,ncol=3,byrow=T)
>
>
> How can I find out, how many times x1 is a row of A?
>
> Thanks in advance and best regards,
> Sebastian
>
> ______________________________________________
> R-help at r-project.org 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