Если вы предпочитаете использовать пакет dplyr
, используйте функцию left_join
:
library(dplyr)
bigDF %>% left_join(lookupDF)
# Joining, by = "Lookup"
# A tibble: 11 x 5
# Species Site Present Lookup Val
# <chr> <chr> <dbl> <chr> <dbl>
# 1 A A1 1 Aa1 12
# 2 A A2 0 Ab2 15
# 3 A A3 1 Aa3 18
# 4 A A4 1 Aa4 101
# 5 A A4.2 1 Aa4 101
# 6 B B1 0 Bb1 60
# 7 B B2 0 Bb2 75
# 8 B B3 0 Bb3 89
# 9 B B4 1 Bb4 3
# 10 B B1.1 1 Bb1 60
# 11 B B2.1 0 Bb2 75
Или используйте базовый R
merge(bigDF, lookupDF)
Примечание. merge
не сохраняет порядок строк
data
bigDF <- tribble(
~Species, ~Site, ~Present, ~Lookup,
"A", "A1", 1, "Aa1",
"A", "A2", 0, "Ab2",
"A", "A3", 1, "Aa3",
"A", "A4", 1, "Aa4",
"A", "A4.2", 1, "Aa4",
"B", "B1", 0, "Bb1",
"B", "B2", 0, "Bb2",
"B", "B3", 0, "Bb3",
"B", "B4", 1, "Bb4",
"B", "B1.1", 1, "Bb1",
"B", "B2.1", 0, "Bb2")
lookupDF <- tribble(
~Lookup, ~Val,
"Aa1", 12,
"Ab2", 15,
"Aa3", 18,
"Aa4", 101,
"Bb1", 60,
"Bb2", 75,
"Bb3", 89,
"Bb4", 3)