I wanted to know the easiest way to do a simple function whose aim is to:
- I want to filter the First data frame using ID* from a second data frame.
- I also want to add a new column to the filtered data from the second column corresponding to the ID* it is linked to.
I've provided a sample code in which maindata
is the primary data and Age* from subdata
has to be added to the maindata
while filtering only the IDs* present in the subdata to have a data frame with ID,Name and Age. This final data frame is named finaldata
Sample code:
library(dplyr)
maindata = data.frame(ID=c(1:10), Name=c("a","b","c","d","e","f","g","h","i","j"))
subdata = data.frame(ID=c(1,3,6,2,7), Age=c(26,34,33,55,21))
#This is what I would like to have
finaldata = data.frame(ID=c(1,3,6,2,7), Name=c("a","c","f","b","7"), Age=c(26,34,33,55,21))
# I've tried using dplyr with no luck, This must be very basic :(
finaldata = maindata %>%
filter(ID %in% subdata$ID) %>%
mutate(Age=subdata$Age)
*Header names from example