Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to Sapply in dplyr

Tags:

r

dplyr

I want to know how we can write similar to sapply in dplyr. Here I am calculating no. of distinct values. I have similar multiple sapply statements so I thought to write using mutate in dplyr.

distinctValues <- sapply(iris, function(var) dplyr::n_distinct(var))
like image 516
Ujjawal Bhandari Avatar asked May 16 '21 16:05

Ujjawal Bhandari


2 Answers

Update: for different names you can use .names = "{.col}.new{.fn}"

iris %>% 
  summarize(across(everything(), n_distinct, .names = "{.col}.new{.fn}"))

We can use summarize with across

library(dplyr)
iris %>% 
  summarize(across(everything(), n_distinct))

Output:

  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1           35          23           43          22       3
like image 95
TarJae Avatar answered Oct 16 '22 19:10

TarJae


In base R, we can do

sapply(iris, function(var) length(unique(var)))
like image 24
akrun Avatar answered Oct 16 '22 17:10

akrun