I'm struggling to replace some of the values in a column in R. In my dataframe, there are more than 100000 rows; however, I'm simplifying things here. Let's consider this imaginary data frame:
ID<-c("Rick","Anna","Sarah","Mike","Kim","Garrett","Tom")
Age<-c(31,23,26,43,32,41,22)
df1<-cbind(ID,Age)
ID Age
Rick 31
Anna 23
Sarah 26
Mike 43
Kim 32
Garrett 41
Tom 22
However, some of age information in this data frame is wrong and df2 has the correct information for age.
ID<-c("Rick","Tom")
Age<-c(35,23)
df2<-cbind(ID,Age)
ID Age
Rick 35
Tom 23
My question is, how can I take the correct values in df2 and supply them to df1?
I have tried the solutions in other similar situations on stackoverflow including:
df1$Age<-df2$Age[df1$ID %in% df2$ID]
df1$Age<-df2$Age[match(df1$ID, df2$ID]
These codes take the values in df2 and returns NAs for IDs that are not available in df2.
I need suggestions, please.