[R] matching and extracting data
Chuck Cleland
ccleland at optonline.net
Fri Nov 27 17:00:22 CET 2009
On 11/27/2009 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
>
> 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
> subset(X, X[,3] %in% Y[,1])
[,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"
> 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.
--
Chuck Cleland, Ph.D.
NDRI, Inc. (www.ndri.org)
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894
More information about the R-help
mailing list