-1

If I have a data frame with multiple columns (which I don't know the exact number of columns) but I know the have common in name the stock_ and the other is a number like this

stock_1        stock_2  stock_3       stock_4
Google         NA       Yahoo         Google
Bing  Search   Yahoo    Bing Search   NA
Google         Yahoo    Yahoo         Bing Search

How is it possible to count the frequency of all words of all columns and take a result like this:

name          frequency
Google           3
Bing Search      3
Yahoo            4
Jaap
  • 81,064
  • 34
  • 182
  • 193
Keri
  • 17
  • 7

1 Answers1

0

We can unlist the dataset and use table

as.data.frame(table(unlist(df1)))
#         Var1 Freq
#1 Bing Search    3
#2      Google    3
#3       Yahoo    4

data

df1 <- structure(list(stock_1 = c("Google", "Bing Search", "Google"), 
stock_2 = c(NA, "Yahoo", "Yahoo"), stock_3 = c("Yahoo", 
"Bing Search", 
"Yahoo"), stock_4 = c("Google", NA, "Bing Search")), 
.Names = c("stock_1", 
"stock_2", "stock_3", "stock_4"), class = "data.frame", 
 row.names = c(NA, -3L))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thank you. This solution gives for var1 numbers 1,2 etc. and not the name of the word. Any idea from where I could see why I have this? – Keri Feb 01 '16 at 18:42
  • @Keri I think the columns are `factor `class. Convert to `character` and it will work. `df1[] <- lapply(df1, as.character)` and then apply the code. – akrun Feb 01 '16 at 18:44