Home > OS >  Getting the count of uniqe observations in a particular column
Getting the count of uniqe observations in a particular column

Time:12-15

I have a data frame that has a column containing multiple-choice responses. The column has this structure

Improvement <- c("Solarisation, Storage expansion, pipeline extension, other",
                 "pipeline extension", "Solarisation, Treatment", "Treatmemt, Other", "pipeline extension, treatment")
Multiple <- data.frame(Improvement)

I want to get the count of unique observations present in the column Improvement.

CodePudding user response:

You can use separate_rows(), i.e.

table(tidyr::separate_rows(Multiple, Improvement) )

   expansion    extension        other        Other     pipeline Solarisation      Storage    Treatmemt    treatment    Treatment 
           1            3            1            1            3            2            1            1            1            1 

CodePudding user response:

You may try(base R)

table(unlist(sapply(Multiple$Improvement, function(x) strsplit(x, ", "))))

     other              Other pipeline extension       Solarisation  Storage expansion          Treatmemt 
         1                  1                  3                  2                  1                  1 
 treatment          Treatment 
         1                  1 

Thanks to @Sotos

table(unlist(strsplit(Multiple$Improvement, ", ")))

CodePudding user response:

Updated to include tolower to avoid counting things twice

 Multiple %>% 
 separate(Improvement, c("A", "B", "C", "D"), sep = ",") %>% 
 mutate_at(vars(c("A", "B", "C", "D")), funs(tolower(.))) %>% 
 gather()  %>% 
 count(value)
  • Related