Alex Fort
Alex Fort

Reputation: 93

R software extracting a subset of rows using an array

I have a set of three Matrix composed by the same columns

confasipri1  
confasipri2  
confasipri3  

One of the column is called ROW_NUM and i found the common values of ROW_NUM using the following statements

metercomuni<-intersect (confasipri1[,"ROW_NUM"],confasipri2[,"ROW_NUM"])   

metercomuni<- intersect (metercomuni,confasipri3[,"ROW_NUM"])  

Now my problem is to extract from confasipri1, confasipri2, confasipri3 all the rows having the metercomuni in common.
As example if in metercomuni i have (1,3,5,8)
then I have to create a new matrix (comuni1) extracting from the confasipri1 only the rows having

ROW_NUM = 1 
ROW_NUM = 3 
ROW_NUM = 5 
ROW_NUM = 8

and the same to create comuni2 from confasipri2 and comuni 3 from confasipri3

confaspri1
row_num  datoA        datoB
1          p           f
2          c           a 
3          h           b
4          i           c
5          m           c

confasipri2
row_num  datoA        datoB
1          s           w
3          d           e
4          f           r
5          g           t

confasipri3
row_num  datoA        datoB
1          q           p
3          a           k
6          z           l
8          v           m

metercomuni  
1 3

the result i am dreaming on is

comuni1 
    row_num  datoA        datoB
    1          p           f
    3          h           b

comuni2 
    row_num  datoA        datoB
    1          s           w
    3          d           e

comuni3
    row_num  datoA        datoB
    1          q           p
    3          a           k

Upvotes: 0

Views: 53

Answers (1)

NicE
NicE

Reputation: 21425

Try this:

 comuni1<-confasipri1[confasipri1[,"row_num"] %in% metercomuni,]

Looks like your data might be data frames not matrixes so you can also do:

comuni1<-with(confasipri1,confasipri1[row_num %in% metercomuni,])

Upvotes: 2

Related Questions