-2

I'm trying to get unique values and count of no of occurrences for a column which has multiple values.

   Genre
1   Action, Thriller
2   Drama, Romance
3   Comedy, Drama
4   Action, Thriller

I have tried using count function after splitting the column Genre.

Getting below error:

count(df[0:4],vars=NULL)
Error in df2[u_id, , drop = FALSE] 
sm925
  • 2,648
  • 1
  • 16
  • 28

2 Answers2

1

dplyr will do that: dplyr::count(df, Genre)

Kent Johnson
  • 3,320
  • 1
  • 22
  • 23
0

There are multiple ways with which you can do that, below are some ways;

Using dplyr

# Call dplyr package
library(dplyr)

# Create dataset
data <-
  data.frame(
    type = c("Action", "Thriller", "Drama", "Drama", "Romance", "Romance",
             "Comedy", "Comedy", "Comedy", "Drama", "Drama", "Drama",
             "Action", "Action", "Action", "Action", "Thriller")
  )

data %>%
  group_by(type) %>% # To count per column called type (can be dropped if there is only type column in the dataframe)
  count() # Count

# A tibble: 5 x 2
# Groups:   type [5]
# type         n
# <fct>    <int>
#   Action       5
#   Comedy       3
#   Drama        5
#   Romance      2
#   Thriller     2

Without need for package

table(data)

# data
# Action   Comedy    Drama  Romance Thriller 
# 5        3        5        2        2 

Using janitor to get percentage as well

janitor::tabyl(data$type)

# data$type n   percent
# Action 5 0.2941176
# Comedy 3 0.1764706
# Drama 5 0.2941176
# Romance 2 0.1176471
# Thriller 2 0.1176471
Nareman Darwish
  • 1,251
  • 7
  • 14
  • The more correct way of doing this is using tally or directly passing the variable to count – Bruno Jan 04 '20 at 20:13
  • This is true, I only grouped by in case there are multiple columns in the dataframe – Nareman Darwish Jan 04 '20 at 20:16
  • 1
    This also works mtcars %>% count(cyl,gear), if you need the groups tally is more sensible since the last group is not really a group anymore see https://github.com/tidyverse/dplyr/issues/862 – Bruno Jan 04 '20 at 20:19