I need to create a unique identifier from the combination of two variables in a data frame. Consider the following data frame:
df <- data.frame(col1 = c("a", "a", "b", "c"), col2 = c("c", "b", "c", "a"), id = c(1,2,3,1))
The variable "id" is not in the data set; that's the one I would like to create. Essentially, I want every combination of the variables col1 and col2 to be treated interchangeably, eg the combination of c("a", "c") is the same as c("c", "a").
You could do:
labels <- apply(df[, c("col1", "col2")], 1, sort)
df$id <- as.numeric(factor(apply(labels, 2, function(x) paste(x, collapse=""))))
A more complicated, but quicker to run version than looping over each row.
sel <- c("col1","col2")
df[sel] <- lapply(df[sel], as.character)
as.numeric(factor(apply(df[1:2], 1, function(x) toString(sort(x)) )))
#[1] 2 1 3 2
as.numeric(interaction(list(do.call(pmin,df[1:2]),do.call(pmax,df[1:2])),drop=TRUE))
#[1] 2 1 3 2
Benchmarking on 1M rows:
df2 <- df[rep(1:4, each=2.5e5),]
system.time(as.numeric(factor(apply(df2[1:2], 1, function(x) toString(sort(x)) ))))
# user system elapsed
# 69.21 0.08 69.41
system.time(as.numeric(interaction(list(do.call(pmin,df2[1:2]),do.call(pmax,df2[1:2])),drop=TRUE)))
# user system elapsed
# 0.88 0.03 0.91
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.