bogdanknez bogdanknez - 1 year ago 71
R Question

Filtering out entries from a table in R

I am analyzing a table in R that has 5 columns: name, seed, weight, priority, and rank (let's call this variable fulltable).

I have also isolated a column ('name') from another table I have using:

candidates <- othertable$name

What I would like to do now is extract all the entries from fulltable where fulltable$name matches any of the entries in candidates. What is important to me is to find out both:

  1. Which entries from candidates are present in fulltable


  1. What the weight, priority, and rank are of those entries.

Essentially, I want to extract the full row of each spot where there is a match between the columns of the two tables.

When I use a function like intersect(fulltable$name, candidates), I get a character string showing me the overlap of the name column (which answers my Q1), but without the other critical information (i.e. no weight, priority, or rank information).

Any and all help is much appreciated!

Answer Source

If I get it right, you might be interested in something like:

subdata <- fulldata[match(candidates, fulldata$name),]

For example:

Supposed you have a data frame fulltable like this:

    Namen Value1 Value2
1     max      1      1
2 Andreas      2      2
3   Achim      3      3
4   micha      4      4
5  Robert      5      5
6    Ralf      6      6
7   manny      7      7

And want to extract the following names:

> candidates
[1] "max"   "micha" "manny"

Then the following line of code results in:

fulltable[match(candidates, fulltable$Namen),]
  Namen Value1 Value2
1   max      1      1
4 micha      4      4
7 manny      7      7
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download