Manipulating a Column with a Concatenated List in R

Viewed 39

I found a way to make it work, but it seems clumsy. There has to be a better way...

The question I might try to answer is If I wanted to find out how often a language was selected by country, how would I do that efficiently?

This works, what's better?

library(tidyverse)

data(SO_survey, package = "crunch")

# what does it look like now?
SO_survey %>% select(Country, WantWorkLanguage) %>% head()

It's set up like this

#                Country                                                              WantWorkLanguage
# 4        United States                                                        Matlab; Python; R; SQL
# 11       United States                                                                    C#; R; SQL
# 36               Italy                                                         JavaScript; Python; R
# 125            Denmark                                Groovy; Java; JavaScript; Lua; SQL; TypeScript
# 242      United States                                                                   C++; Python
# 298 Dominican Republic C; C#; CoffeeScript; Go; Haskell; JavaScript; Perl; PHP; Python; R; Ruby; SQL 

Made a unique list of languages

# extract unique languages
(wanted = SO_survey %>% 
  select(WantWorkLanguage) %>% 
  unlist() %>% 
  strsplit("; ", fixed = T) %>%
  unlist() %>%
  unique()

To extract the county by one country

# how often did a respondent pick a particular language in the US?
SO_survey %>% 
  filter(Country == "United States") %>% 
  {strsplit(unlist(.$WantWorkLanguage),"; ",fixed = T)} %>% 
  unlist() %>% 
  table(. %in% wanted)

If I want it arranged/sorted

# If I want it sorted or arranged 
SO_survey %>% 
  filter(Country == "United States") %>% 
  {strsplit(unlist(.$WantWorkLanguage),"; ",fixed = T)} %>% 
  unlist() %>% 
  table(. %in% wanted) %>% 
  data.frame() %>% 
  select(-Var2) %>% 
  arrange(-Freq)

The output:

#                 . Freq
# 1          Python  321
# 2               R  288
# 3             SQL  209
# 4      JavaScript  199
# 5             C++  136
# 6            Java  115
# 7              Go  101
# 8              C#  100
# 9           Scala   81
# 10              C   74
# 11          Swift   57
# 12          Julia   56
# 13     TypeScript   55
# 14        Haskell   52
# 15           Rust   38
# 16             F#   36
# 17            PHP   32
# 18           Ruby   32
# 19       Assembly   29
# 20        Clojure   29
# 21         Matlab   29
# 22         Elixir   23
# 23           Perl   18
# 24    Objective-C   17
# 25   CoffeeScript   16
# 26         Erlang   16
# 27            Lua   13
# 28    Common Lisp   12
# 29            VBA   11
# 30         Groovy    7
# 31           Dart    5
# 32      Smalltalk    3
# 33         VB.NET    3
# 34           Hack    2
# 35 Visual Basic 6    1 
1 Answers

Your tidyverse solution seems pretty good. For something more concise you could try base R or data.table:

library(data.table)
setDT(SO_survey)
setkey(SO_survey, Country)
SO_survey['United States', .(lang = unlist(strsplit(WantWorkLanguage, '; ')))
          ][, .N, keyby = V1 ][order(-N)]

 #              lang   N
 # 1:         Python 321
 # 2:              R 288
 # 3:            SQL 209
 # 4:     JavaScript 199
 # 5:            C++ 136
 # ...
Related