-2

I have a list of names in csv file(file a), it has only names. Other csv file (file b) has also several names in the first column, it has 10 columns in total. I want to analyze the first column of file b, search for the names in file a and when they are matched(i th element of file a and j th element of the first column of file b), I want to pick all of the row and put it into an empty data frame. I will further use this data frame as a csv file. How can I do that? Thanks in advance.

nkabo
  • 15
  • 3
  • 2
    Please provide a minimal example of dataframe a and b, and a third dataframe with the desired output. – Timo Kvamme Aug 03 '16 at 11:25
  • You can refer to [How to make a great R reproducible example?](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for tips on producing a minimal example. – Weihuang Wong Aug 03 '16 at 11:40

1 Answers1

0

Ideally, you should have shared a reproducible example to play with. However, I will do that for you using the built - in mtcars dataset.Have done this based on my understanding of your question.

Data Preparation

#Taking any random 6 sample rownames from mtcars dataset. Just to generalize making
#it as 3 X 2 dataframe

fileA <- data.frame(matrix(sample(row.names(mtcars), 6), nrow = 3))
fileA
#        X1                 X2
#1   AMC Javelin       Ferrari Dino
#2 Porsche 914-2  Chrysler Imperial
#3    Merc 450SE Cadillac Fleetwood

#Making first column as rownames of mtcars and then removing the rownames
fileB <- cbind(Column1 = rownames(mtcars), mtcars)
rownames(fileB) <- NULL


#    Column1            mpg   cyl  disp   hp  drat    wt  qsec vs am gear carb
#27  Porsche 914-2      26.0   4   120.3  91  4.43  2.140 16.7  0  1    5    2
#28  Lotus Europa       30.4   4   95.1   113 3.77  1.513 16.9  1  1    5    2
#29  Ford Pantera L     15.8   8   351.0  264 4.22  3.170 14.5  0  1    5    4
#30  Ferrari Dino       19.7   6   145.0  175 3.62  2.770 15.5  0  1    5    6
#31  Maserati Bora      15.0   8   301.0  335 3.54  3.570 14.6  0  1    5    8
#32  Volvo 142E         21.4   4   121.0  109 4.11  2.780 18.6  1  1    4    2

and finally now you can use,

fileB[fileB$Column1 %in% unlist(fileA), ]

#            Column1    mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#12         Merc 450SE 16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3     
#15 Cadillac Fleetwood 10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
#17  Chrysler Imperial 14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
#23        AMC Javelin 15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
#27      Porsche 914-2 26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
#30       Ferrari Dino 19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213