1 Loading the required libraries

First step load the library to be used for the assesment

2 reading the files

I will connect the files, read the entire file, randomly choose 1000 rows of text.

3 Cleaning the data

Now I will remove the most common offensive words and N/A from the lists and later transform the lists to databases

4 count the words

Now I will count the frequencies of the words for each dataset and choose those values with more than 100 occurrences

# Subset news_df

a <- unnest_tokens(news_df, "words", "news")
count_nw <- count(a, words)
count_nw <- subset( count_nw , n >=100)

#Subset blog_df
a <- unnest_tokens(blog_df, "words", "blog")
count_blog <- count(a, words)
count_blog <- subset( count_blog , n >=100)

#Subset tweet_df
a <- unnest_tokens(tweet_df, "words", "twitt")
count_tw <- count(a, words)
count_tw <- subset( count_tw , n >=100)

rm(a)

5 Combining the datasets

I will combine the datasets with more than 100 occurencies, sort by the word column , group them, sum the occurrencies and order in decreasing order. I will then write a script to print the first 10 results of the most frequent words in the combined dataset.

combi <- bind_rows(count_blog, count_nw, count_tw)
combi <- combi[order(combi$words), ]
combi <- group_by(combi, words) %>% mutate(n = sum(n))  %>% select(words, n)
combi <- unique(combi)
combi <- combi[order(combi$n, decreasing = TRUE),]
mx <- head(combi, 10)

The 10 most frequent words in the dataset:

## 1 °  most frequent word is:   " the ",  shown  4110  times 
##  2 °  most frequent word is:   " to ",  shown  2286  times 
##  3 °  most frequent word is:   " and ",  shown  2173  times 
##  4 °  most frequent word is:   " a ",  shown  2057  times 
##  5 °  most frequent word is:   " of ",  shown  1819  times 
##  6 °  most frequent word is:   " in ",  shown  1461  times 
##  7 °  most frequent word is:   " i ",  shown  1319  times 
##  8 °  most frequent word is:   " for ",  shown  903  times 
##  9 °  most frequent word is:   " is ",  shown  849  times 
##  10 °  most frequent word is:   " that ",  shown  775  times

6 plot of the data

I will now plot the data in four different plots, one for each dataset (news, blog and tweet) and one for the combined dataset

7 Split in tokens 2grams and 3grams

I will now combined together the 3 full datasets, tokenize to 2 and 3 grams tokens, change the name of the column, sort them and take the first 20 results for each resulting dataset. Later I will print the results of both datasets with the sentences that appear more frequently

Frequency of the 2 words sentence in the combined database:

## 1 °  most frequent token is:   " of the ",  shown  391  times 
##  2 °  most frequent token is:   " in the ",  shown  336  times 
##  3 °  most frequent token is:   " to the ",  shown  178  times 
##  4 °  most frequent token is:   " to be ",  shown  158  times 
##  5 °  most frequent token is:   " for the ",  shown  156  times 
##  6 °  most frequent token is:   " on the ",  shown  147  times 
##  7 °  most frequent token is:   " at the ",  shown  129  times 
##  8 °  most frequent token is:   " in a ",  shown  126  times 
##  9 °  most frequent token is:   " and the ",  shown  105  times 
##  10 °  most frequent token is:   " of a ",  shown  93  times 
##  11 °  most frequent token is:   " with the ",  shown  93  times 
##  12 °  most frequent token is:   " as a ",  shown  79  times 
##  13 °  most frequent token is:   " i was ",  shown  79  times 
##  14 °  most frequent token is:   " for a ",  shown  75  times 
##  15 °  most frequent token is:   " i have ",  shown  75  times 
##  16 °  most frequent token is:   " is a ",  shown  73  times 
##  17 °  most frequent token is:   " will be ",  shown  72  times 
##  18 °  most frequent token is:   " with a ",  shown  72  times 
##  19 °  most frequent token is:   " it was ",  shown  71  times 
##  20 °  most frequent token is:   " from the ",  shown  70  times

Frequency of the 3 words sentence in the combined database:

## 1 °  most frequent token is:   " one of the ",  shown  36  times 
##  2 °  most frequent token is:   " according to the ",  shown  16  times 
##  3 °  most frequent token is:   " going to be ",  shown  15  times 
##  4 °  most frequent token is:   " part of the ",  shown  15  times 
##  5 °  most frequent token is:   " to be a ",  shown  15  times 
##  6 °  most frequent token is:   " as well as ",  shown  13  times 
##  7 °  most frequent token is:   " this is a ",  shown  13  times 
##  8 °  most frequent token is:   " i need to ",  shown  12  times 
##  9 °  most frequent token is:   " out of the ",  shown  12  times 
##  10 °  most frequent token is:   " a lot of ",  shown  11  times 
##  11 °  most frequent token is:   " have to be ",  shown  11  times 
##  12 °  most frequent token is:   " i don't think ",  shown  11  times 
##  13 °  most frequent token is:   " thanks for the ",  shown  11  times 
##  14 °  most frequent token is:   " the fact that ",  shown  11  times 
##  15 °  most frequent token is:   " i have a ",  shown  10  times 
##  16 °  most frequent token is:   " in the middle ",  shown  10  times 
##  17 °  most frequent token is:   " the end of ",  shown  10  times 
##  18 °  most frequent token is:   " the rest of ",  shown  10  times 
##  19 °  most frequent token is:   " there was a ",  shown  10  times 
##  20 °  most frequent token is:   " to have a ",  shown  10  times

8 end of project

Many thanks to all