mardi 22 décembre 2020

R or SQL - Create a new column based on mapping of another dataframe

I've attached screenshots of my mapping dataframe:

mapping

as well as a screenshot of a larger data frame that I'm trying to map:

data

is there any code I could use to get an output like so?:

output

This is the logic I'm basing it off:

1.If a row in my larger data frame has a dx1 OR dx2 OR dx3 code that matches to a corresponding dx code found in my mapping table FOR a specific proc2 value (also found in mapping table) AND it is within the pre_date and post_date window of that proc2 value, THEN append that proc2 value into the trigger_event column (trigger_event).

  1. If a row in my larger data frame has a dx1 OR dx2 OR dx3 code that matches to a corresponding dx code found in my mapping table FOR a specific proc2 value AND it is within the pre_date and post_date window of that proc2 value, THEN count it as 1 category (Cat_1).

  2. There are instances as can be seen in my desired output where a pre_date or post_date might overlap with other rows corresponding to other proc2 values, so I would like to create another column where that instance can be seen (Cat_2)

Is there any way I could do this in R or even SQL? I'm so sorry I didn't provide example code! But an overall direction/advice would be so helpful if that's all that can be provided. Thanks so much in advance!!!

Aucun commentaire:

Enregistrer un commentaire