Yves
Yves

Reputation: 556

How to count unique values over multiple columns using R?

Let's say I have the following df:

1               2                    3
home, work      work, home           home, work
leisure, work   work, home, leisure  work, home
home, leisure   work, home           home, work

I want to count all unique variables over the entire data.frame (not by columns or row, I'm interested in the cell values)

So the output should look like this:

                    freq
home, work          3
leisure, work       1
home, leisure       1
work, home          3
work, home, leisure 1

I have not found a way to do that. The count() function seems to work only with single columns.

Thank you very much for the help:)

Upvotes: 0

Views: 205

Answers (2)

akrun
akrun

Reputation: 886938

With tidyverse, we can use gather

library(dplyr)
library(tidyr)
df %>% 
   gather %>% 
   count(value)
#              value n
#1      home,leisure 1
#2         home,work 3
#3      leisure,work 1
#4         work,home 3
#5 work,home,leisure 1

data

df <- structure(list(X1 = c("home,work", "leisure,work", "home,leisure"
), X2 = c("work,home", "work,home,leisure", "work,home"), X3 = c("home,work", 
"work,home", "home,work")), class = "data.frame", row.names = c(NA, -3L))

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 388807

You could unlist and use table to get count in base R :

stack(table(unlist(df)))
#Same as
#stack(table(as.matrix(df)))

If you prefer tidyverse get data in long format using pivot_longer and count.

df %>%
  tidyr::pivot_longer(cols = everything()) %>%
  dplyr::count(value)

# A tibble: 5 x 2
#  value                 n
#  <chr>             <int>
#1 home,leisure          1
#2 home,work             3
#3 leisure,work          1
#4 work,home             3
#5 work,home,leisure     1

data

df <- structure(list(X1 = c("home,work", "leisure,work", "home,leisure"
), X2 = c("work,home", "work,home,leisure", "work,home"), X3 = c("home,work", 
"work,home", "home,work")), class = "data.frame", row.names = c(NA, -3L))

Upvotes: 1

Related Questions