Это может быть то, что вы хотите, хотя столбец name
не находится в конечном выводе.
library(tidyverse)
df3 <- df1 %>%
rowid_to_column() %>%
gather(type, name, -age, -rowid) %>%
left_join(df2, by = "name") %>%
group_by(rowid) %>%
mutate(age1 = ifelse(is.na(age1), unique(age1[!is.na(age1)]), age1)) %>%
spread(type, name) %>%
ungroup()
df3
# # A tibble: 7 x 5
# rowid age age1 first.name full.name
# <int> <int> <int> <chr> <chr>
# 1 1 10 10 bob bob marley
# 2 2 12 12 jus jus bieber
# 3 3 14 23 xyz xyz abcdef
# 4 4 15 21 abc abc qwerty
# 5 5 10 10 hey hey hello
# 6 6 12 12 jack jack ma
# 7 7 15 17 mark zuke mark
ДАННЫЕ
df1 <- read.table(text = "full.name first.name age
'bob marley' bob 10
'jus bieber' jus 12
'xyz abcdef' xyz 14
'abc qwerty' abc 15
'hey hello' hey 10
'jack ma' jack 12
'zuke mark' mark 15",
header = TRUE, stringsAsFactors = FALSE)
df2 <- read.table(text = "name age1
'asd dfg' 23
bob 10
'jus bieber' 12
xyz 23
'abc qwerty' 21
'hey hello' 10
jack 12
'zuke mark' 17
'bradd pit' 50",
header = TRUE, stringsAsFactors = FALSE)