[R] matrix merge problem
Luc Villandre
villandl at dms.umontreal.ca
Tue Apr 14 22:32:39 CEST 2009
Hi Juan,
Your first question can be answered easily.
bin.matrix = matrix(A %in% B,nrow(A),ncol(A)) ;
bin.vector = rowSums(bin.matrix) > 0 ;
C = A[bin.vector,] ;
This should do the trick.
Cheers,
Luc
Juan Pablo Fededa wrote:
> Dear r-help contributors,
>
> I have two questions:
>
> first:
>
> I have a matrix A and a vector B.
> I want to make a new matrix C, which is made of the rows of A having a value
> included in B.
>
> Second:
>
> I have two matrixes A and B, of different dimensions.
> B has unique values in column 2 and A has not unique values on column 2.
> I want to merge this two matrixes by the values in the columns 2 of B and 2
> of A in such a way that the resulting matrix will be C:
>
>
> A:
>
> 1 2
> 2 2
> 3 3
> 4 3
> 5 4
> 6 4
> 7 5
> 8 5
>
>
> B:
>
> 1 2 x c d
> 2 3 y f g
>
> C:
>
> 1 2 x c d
> 2 2 x c d
> 3 3 y f g
> 4 3 y f g
> 5 4 NA NA
> 6 4 NA NA
> 7 5 NA NA
> 8 5 NA NA
>
>
> Can you help me?
>
>
> Many thanks in advance.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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