I have the following data and wish to create an $ID
variable for each unique interaction between two columns
DATE <- c('V', 'V', 'W', 'W', 'X', 'X', 'Y', 'Y', 'Z', 'Z')
SEX <- rep(1:2, 5)
Blood_T1 <- c(3,4,3,3,4,3,1,6,3,4)
Blood_T2 <- c(4,3,3,3,3,4,6,1,4,3)
df1 <- data.frame(DATE, SEX, Blood_T1, Blood_T2)
When grouping by $DATE
, I want to create a new dummy variable for each unique combination of $Blood_T1
and $Blood_T2
regardless of their order.
The desired out appears below:
I cant use the sum, as it does not always produce unique combinations. (See the part marked in yellow above for clarification)
I have tried the following commands but have not yet hit the nail on the head
with(df1, interaction(Blood_T1, Blood_T2))
as.numeric(as.factor(with(df1, paste(Blood_T1, Blood_T2))))
transform(df1, Cluster_ID = as.numeric(interaction(Blood_T1, Blood_T2, drop=TRUE)))