I have 2 dataframes:
df1 = data.frame(Bird_ID = c(1:6), Sex = c("Male","Female","Male","Male","Male","UNK"))
df2 = data.frame(Bird_ID = c(6), Seen_sex = c("Female"))
df1
# Bird_ID Sex
# 1 Male
# 2 Female
# 3 Male
# 4 Male
# 5 Male
# 6 UNK
df2
# Bird_ID Seen_Sex
# 6 Female
df1
) is my database where I have all my birds
with known sex.df2
) is the "updater"How can I update the bird 6 in df1
using information from df2
? So "UNK" in df1 should become "Female" now and all other birds stay the same.
You may use match
in base R -
df1$Sex[match(df2$Bird_ID, df1$Bird_ID)] <- df2$Seen_sex
df1
# Bird_ID Sex
#1 1 Male
#2 2 Female
#3 3 Male
#4 4 Male
#5 5 Male
#6 6 Female