calculating the number frequency in variables of data frame

Hi I have a data frame like below ,I am trying to get the count entries of variables .
I want to create a function for that

df <- data.frame(uniq_id=c(9143,2357,4339,8927,9149,4285,2683,8217,3702,7857,3255,4262,8501,7111,2681,6970),
                   name=c("xly,mnn","xab,Lan","mhy,mun","vgtu,mmc","ftu,sdh","kull,nnhu","hula,njam","mund,jiha","htfy,ntha","sghu,njui","sgyu,hytb","vdti,kula","mftyu,huta","mhuk,ghul","cday,bhsue","ajtu,nudj"),
                   city=c("A","B","C","C","D","F","S","C","E","S","A","B","W","S","C","A"),
                   age=c(22,45,67,34,43,22,34,43,34,52,37,44,41,40,39,30),
                   country=c("usa","USA","AUSTRALI","AUSTRALIA","uk","UK","","","CHINA","CHINA","BRAZIL","BRASIL","CHILE","","CANADA","UK"),
                   lang=c("ENGLISH(US)","","ENGLISH","ENGLISH","ENGLISH(UK)","","SPANISH","SPANISH","CHINESE","","ENGLISH","ENGLISH","","ENGLISH","",""),
                   gender=c("MALE","","male","m","","MALE","FEMALE","f","FEMALE","MALE","MALE","","FEMALE","FEMALE","MALE","MALE"),
                   company =c("Bank fíSç","","ABC","Core/Field","Core/Field","","Core-Field","CONSULTANCY","Bank fíSç","Bank fíSç","Bank fíSç","","HR","CONST.","","Bank fíSç"),
                   property = c("Bank fíSç","","ABC","Core/Field","Core/Field","","Core-Field","CONSULTANCY","Bank fíSç","Bank fíSç","Bank fíSç","","HR","CONST.","","Bank fíSç"),
                   car =c("yes","no","yes","","yes","no","yes","no","no","","yes","","no","yes","yes","Yes"),
                   child =c("1","3","","0","2","","","1","0","2","","","1","2","2","1"))

I have tried with janitor::tabyl also with summarise but not working.
so the out put should be like exported in excel like below

for example

column option count
country usa 2
country australia 2
country uk 3
country china 2
country brazil 2
country chile 1
country canada 1
language english(us) 1
language english(uk) 1
language english 5
language spanish 2
language chinese 1
suppressPackageStartupMessages({library(dplyr)})
# prefer `dat` over `df`, which is a built-in function
dat <- data.frame(
  uniq_id = c(9143, 2357, 4339, 8927, 9149, 4285, 2683, 8217, 3702, 7857, 3255, 4262, 8501, 7111, 2681, 6970),
  name = c("xly,mnn", "xab,Lan", "mhy,mun", "vgtu,mmc", "ftu,sdh", "kull,nnhu", "hula,njam", "mund,jiha", "htfy,ntha", "sghu,njui", "sgyu,hytb", "vdti,kula", "mftyu,huta", "mhuk,ghul", "cday,bhsue", "ajtu,nudj"),
  city = c("A", "B", "C", "C", "D", "F", "S", "C", "E", "S", "A", "B", "W", "S", "C", "A"),
  age = c(22, 45, 67, 34, 43, 22, 34, 43, 34, 52, 37, 44, 41, 40, 39, 30),
  country = c("usa", "USA", "AUSTRALI", "AUSTRALIA", "uk", "UK", "", "", "CHINA", "CHINA", "BRAZIL", "BRASIL", "CHILE", "", "CANADA", "UK"),
  lang = c("ENGLISH(US)", "", "ENGLISH", "ENGLISH", "ENGLISH(UK)", "", "SPANISH", "SPANISH", "CHINESE", "", "ENGLISH", "ENGLISH", "", "ENGLISH", "", ""),
  gender = c("MALE", "", "male", "m", "", "MALE", "FEMALE", "f", "FEMALE", "MALE", "MALE", "", "FEMALE", "FEMALE", "MALE", "MALE"),
  company = c("Bank fíSç", "", "ABC", "Core/Field", "Core/Field", "", "Core-Field", "CONSULTANCY", "Bank fíSç", "Bank fíSç", "Bank fíSç", "", "HR", "CONST.", "", "Bank fíSç"),
  property = c("Bank fíSç", "", "ABC", "Core/Field", "Core/Field", "", "Core-Field", "CONSULTANCY", "Bank fíSç", "Bank fíSç", "Bank fíSç", "", "HR", "CONST.", "", "Bank fíSç"),
  car = c("yes", "no", "yes", "", "yes", "no", "yes", "no", "no", "", "yes", "", "no", "yes", "yes", "Yes"),
  child = c("1", "3", "", "0", "2", "", "", "1", "0", "2", "", "", "1", "2", "2", "1")
)

dat %>% group_by(country,lang) %>% count()
#> # A tibble: 14 x 3
#> # Groups:   country, lang [14]
#>    country     lang              n
#>    <chr>       <chr>         <int>
#>  1 ""          "ENGLISH"         1
#>  2 ""          "SPANISH"         2
#>  3 "AUSTRALI"  "ENGLISH"         1
#>  4 "AUSTRALIA" "ENGLISH"         1
#>  5 "BRASIL"    "ENGLISH"         1
#>  6 "BRAZIL"    "ENGLISH"         1
#>  7 "CANADA"    ""                1
#>  8 "CHILE"     ""                1
#>  9 "CHINA"     ""                1
#> 10 "CHINA"     "CHINESE"         1
#> 11 "uk"        "ENGLISH(UK)"     1
#> 12 "UK"        ""                2
#> 13 "usa"       "ENGLISH(US)"     1
#> 14 "USA"       ""                1
dat %>% group_by(lang,country) %>% count()
#> # A tibble: 14 x 3
#> # Groups:   lang, country [14]
#>    lang          country         n
#>    <chr>         <chr>       <int>
#>  1 ""            "CANADA"        1
#>  2 ""            "CHILE"         1
#>  3 ""            "CHINA"         1
#>  4 ""            "UK"            2
#>  5 ""            "USA"           1
#>  6 "CHINESE"     "CHINA"         1
#>  7 "ENGLISH"     ""              1
#>  8 "ENGLISH"     "AUSTRALI"      1
#>  9 "ENGLISH"     "AUSTRALIA"     1
#> 10 "ENGLISH"     "BRASIL"        1
#> 11 "ENGLISH"     "BRAZIL"        1
#> 12 "ENGLISH(UK)" "uk"            1
#> 13 "ENGLISH(US)" "usa"           1
#> 14 "SPANISH"     ""              2

# further processing depending on decision logic

Created on 2020-09-23 by the reprex package (v0.3.0.9001)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.