Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set unique ID for recurring names within column [duplicate]

Tags:

r

My data consists of samples, varying in type, on patients over time. This data is over 10197 observations long. A (small) example of my data is:

PatientName <- c("Jones", "Jones", "Jones", "Smith", "Smith", "Nixon", "Nixon", "Nixon")
SampleType <- c("Venous", "Arterial", "Capillary", "Venous", "Venous", "Venous", "Venous", "Capillary")
DayTested <- c("Monday", "Tuesday", "Wednesday", "Monday", "Monday", "Monday", "Monday", "Tuesday")

df <- data.frame(PatientName, SampleType, DayTested)

I now wish to include a unique ID for when there are repeat sample types on the same patient on the same day.

My anticipated output would be:

df$ID <- c(1,1,1,1,2,1,2,1)

This picks up repeat occurrences of "Smith" and "Nixon" who have repeat "Venous" samples taken on a "Monday" designated by the ID = 2. All other ID's would be equal to 1 as they are seperate samples, taken on seperate days.

Is this please possible to do in R?

like image 874
user2716568 Avatar asked Dec 14 '25 17:12

user2716568


1 Answers

We can use ave

df$ID <- with(df, as.integer(ave(as.character(SampleType),
         PatientName, DayTested, FUN = seq_along)))
df$ID
#[1] 1 1 1 1 2 1 2 1

Or as @lmo suggested

df$ID <- with(df, ave(as.integer(SampleType), PatientName, DayTested, FUN = seq_along))
like image 87
akrun Avatar answered Dec 16 '25 14:12

akrun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!