mercredi 16 mars 2022

R, find pattern and create index, then filter another column based on the created index

I have a value df df1 = data.frame(ID = c("A1; A2; A3; A4", "B1; B2; B3", "C1; C2","D1"), Value = c("1; 2; 3; 4", "5; 6; 7", "8; 9", "10")).

I have another df df2 = data.frame(ID = c("A2", "B3", "C2", "D1")).

Now I would like to write a function to map the 'ID' from df2 to 'ID' in df1, then filter the Value based on the ith position of ID column. The expected output is filtered_df1 = data.frame(ID=c("A2", "B3", "C2", "D1"), value = c("2", "7", "9", "10")).

Would you be able to give any suggestion to create R function?

Thank you so much!

Aucun commentaire:

Enregistrer un commentaire