0

I have a column that has string observations and I need to count the unique words in that column. For e.g.

enter image description here

I would like my final output to look like this-

enter image description here

The words in the column are separate using blanks so that is another challenge in my case.

Thanks

YowE3K
  • 23,852
  • 7
  • 26
  • 40

1 Answers1

1

Here is a solution.

# the data
dat <- data.frame(X = c("Program Manager", "Program Coordinator",
                        "Senior Manager", "Senior Associate",
                        "Senior Researcher"),
                  stringsAsFactors = FALSE)

# count words  
table(unlist(strsplit(dat$X, " +")))

The result:

  Associate Coordinator     Manager     Program  Researcher      Senior 
          1           1           2           2           1           3 

With as.data.frame the result can be converted to a data frame.

tab <- table(unlist(strsplit(dat$X, " +")))
as.data.frame(tab)

The result:

         Var1 Freq
1   Associate    1
2 Coordinator    1
3     Manager    2
4     Program    2
5  Researcher    1
6      Senior    3
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168