-1

I wanted to know the easiest way to do a simple function whose aim is to:

  1. I want to filter the First data frame using ID* from a second data frame.
  2. 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

smci
  • 32,567
  • 20
  • 113
  • 146
Vedha Viyash
  • 708
  • 1
  • 8
  • 19

1 Answers1

0

You can do inner_join between the two datasets based on ID

library(dplyr)
inner_join(maindata, subdata)

Joining, by = "ID"
   ID Name Age
1  1    a  26
2  2    b  55
3  3    c  34
4  6    f  33
5  7    g  21
A. Suliman
  • 12,923
  • 5
  • 24
  • 37