[R] matching and extracting data
David Winsemius
dwinsemius at comcast.net
Fri Nov 27 17:03:17 CET 2009
On Nov 27, 2009, at 10:25 AM, ram basnet wrote:
> Dear all,
> I have querry on how to extract the data by matching between two
> data set where one has the same elements multiple times?
>
> For example, I have two matrix X and Y.
>> X
> [,1] [,2] [,3]
> 1 A 5 P
> 2 B 6 P
> 3 C 7 P
> 4 D 5 Q
> 5 E 6 Q
> 6 F 7 Q
> 7 G 5 R
> 8 H 6 R
> 9 I 7 S
> 10 J 5 S
> 11 K 6 T
> 12 L 7 T
>
> and
>
>> Y [,1]
> 1 P
> 2 Q
> 3 R
> 4 S
>
> Now, I want to select and extract all the data of P, Q, R and S
> elements of column 3 of X matrix by matching with column 1 of Y
> matrix like below:
>
>> [,1] [,2] [,3]
> 1 A 5 P
> 2 B 6 P
> 3 C 7 P
> 4 D 5 Q
> 5 E 6 Q
> 6 F 7 Q
> 7 G 5 R
> 8 H 6 R
> 9 I 7 S
> 10 J 5 S
Perhaps (untested):
X[ X[,3] %in% Y[,1] , ]
Would have been tested if you had used dput or dump on your matrices.
--
David.
>
> I guess, the answer might be simple but i am not getting way to
> figure out. And, i have to select subset from very huge data set.
> So, i need some kinds of automated procedure.
> If some one can help me, it will be great
>
> Thanks in advance.
>
> Sincerely,
> Ram Kumar Basnet
>
>
>
>
> [[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.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
More information about the R-help
mailing list