Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

match_df

Extract matching rows of a data frame.


Description

Match works in the same way as join, but instead of return the combined dataset, it only returns the matching rows from the first dataset. This is particularly useful when you've summarised the data in some way and want to subset the original data by a characteristic of the subset.

Usage

match_df(x, y, on = NULL)

Arguments

x

data frame to subset.

y

data frame defining matching rows.

on

variables to match on - by default will use all variables common to both data frames.

Details

match_df shares the same semantics as join, not match:

  • the match criterion is ==, not identical).

  • it doesn't work for columns that are not atomic vectors

  • if there are no matches, the row will be omitted'

Value

a data frame

See Also

join to combine the columns from both x and y and match for the base function selecting matching items

Examples

# count the occurrences of each id in the baseball dataframe, then get the subset with a freq >25
longterm <- subset(count(baseball, "id"), freq > 25)
# longterm
#             id freq
# 30   ansonca01   27
# 48   baineha01   27
# ...
# Select only rows from these longterm players from the baseball dataframe
# (match would default to match on shared column names, but here was explicitly set "id")
bb_longterm <- match_df(baseball, longterm, on="id")
bb_longterm[1:5,]

plyr

Tools for Splitting, Applying and Combining Data

v1.8.6
MIT + file LICENSE
Authors
Hadley Wickham [aut, cre]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.