library(dplyr)
I have the following data set
set.seed(123)
n <- 1e6
d <- data.frame(a = letters[sample(5, n, replace = TRUE)], b = letters[sample(5, n, replace = TRUE)], c = letters[sample(5, n, replace = TRUE)], d = letters[sample(5, n, replace = TRUE)])
And I would like to count the number of distinct letters in each row. To do this I use
sapply(as.data.frame(t(d)), function(x) n_distinct(x))
However because this approach is implementing a loop, it is slow. Do you have an suggestions on how to speed this up?
My laptop is a piece of junk so...
system.time(sapply(as.data.frame(t(d)), function(x) n_distinct(x)))
user system elapsed
185.78 0.86 208.08
If the different values are not so many, you can try:
d<-as.matrix(d)
uniqueValues<-unique(as.vector(d))
Reduce("+",lapply(uniqueValues,function(x) rowSums(d==x)>0))
For the example you provided, this is much faster than other solutions and yields the same result.
You can try,
system.time(colSums(apply(d, 1, function(i) !duplicated(i))))
#user system elapsed
#6.50 0.02 6.53
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With