I have a dataframe with eight columns that symbolizes the type of mask that each patient has used (Delivery 1, 2, 3, etc.) and another column with the user number.
To avoid saturating the question, I show you here an extract with multiple assumptions of what the table is.
Usuario Entrega 1 Entrega 2 Entrega 3
5 o NA NA
10 FF FF O
12 FF FF FF
20 N FF FF
Each patient can use a type of mask until the end, change, or simply not register until the end.
In any case, what I would like to know is if it is possible to add a new column that means 1 "change" and 0 "no change" for each of the patients depending on whether or not they have changed their mask (regardless of whether they have a record to the end or not).
Is there a function that allows to evaluate if there is a value change between multiple columns?
Thanks in advance.
With base R you simply count by row the unique values of the columns of interest, ignoring the
NA
:One solution I can think of with tidyverse is the following:
Convert the data to Long format:
Group by user and generate the changes variable, which will be "no", when the different values for each delivery value is 1, and "yes" otherwise
Finally return to the Width format, recovering the changes variable
I leave you another very simplified way also using only tidyverse