Performing a simple count, how to count particular entries as 2 and others as 1

Dear R Community,

I have a dataset of drugs and need to count how many each individual has been prescribed using:

drugs_filtered >%>
distinct(prochi, approved_name) >%>
count(approved_name, sort = TRUE -> table_1

BUT some of the drugs (defined as approved_name variable) are combination drugs and for a small number of drugs I need to count them twice. For example, co-codamol. Can I write a caveat into this code so that these combination drugs count = 2 and ll other drugs count = 1?

Many thanks,


You could create new data. One new column splits the drug name by '-', another new column counts the number of elements or drug names in the first column. Then you can sum the second column and get the total number of drugs used.

Thanks very much. Would be difficult to do as many of the combination drugs don't have a "-" so would have to work through manually?

There are probably 20 drugs that are combination - is there a way to add this to the existing code and ask R to count these specific names with a value or 2 and default for all the others as 1?

Many thanks

Unless I can create a new variable for all data frame called drugs_filtered where the default is 0 (ie there is no second drug component) and then I write code to include any of the 20 or so drugs where there are two drugs? Not quite sure how I would do that - would it be mutate() function?

For example, do I want to ask R to mutate a new column and only populate the new variable approved_name_combination for certain drugs (where the original variable approved_name == "CO-CODAMOL")?

mutate(approved_name_combination... )? how to reference the approved_name variable and the caveat to include only some of the drugs?

Many thanks,


Without looking at data, it would be difficult to propose a solution. Can you run an n_distinct on the approved_name column to see how many combinations there are?

Thanks - yes I can but then we are aggregating the figures for each individual and performing logistic regression according to individual characteristics so it needs to be individual level counts.

I am really new to R so making a new column is really difficult! Is it something like

mutate(approved_name_combination = approved_name IF *it happens to be called co-codamol), just don't know how to do the IF thing? Presumably then for drugs not called co-codamol then the new variable would have a value of 0?

Many thanks again, really appreciate your help.


This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.