vlookup: Lookup and retrieve data from a specific column in table

View source: R/vlookup.R

vlookupR Documentation

Lookup and retrieve data from a specific column in table

Description

If you've ever used excel, then you are hopefully familiar with the useful vlookup. This is a fun little version of it in R that was tweeted by Jenny Bryan at some point.

Match on the key and return value.

Usage

vlookup(this, data, key, value)

Arguments

this

Value or vector; what you are trying to look up

data

Data frame; the data where you want to match this

key

The name (quoted) of the column to match on

value

The name (quoted) of the column to return

Value

A vector of values

References

https://twitter.com/JennyBryan/status/980978609794895872/photo/1

Examples

library(dplyr)
head(starwars)

c("Luke Skywalker", "Jabba Desilijic Tiure", "Yoda") %>%
  vlookup(this = .,
          data = starwars,
          key = "name",
          value = "species")

c("Luke Skywalker", "Jabba Desilijic Tiure", "Yoda") %>%
  vlookup(this = ., data = starwars, key = "name", value = "mass") %>%
  sum(.)

c("Luke Skywalker", "Jabba Desilijic Tiure", "Yoda") %>%
  vlookup(data = starwars, key = "name", value = "mass") %>%
  sum(.)

emilelatour/lamisc documentation built on April 9, 2024, 10:33 a.m.