# Perform Topic Modeling By Creating a Function and Pass Along to Purrr : map() using R

My goal is to perform topic modeling (splitting into 3 topics each) by grouping words by word.trend column rather than copy and pasting block of codes for each word.trend as I have 800 unique values for word.trend

I have unnested the text using the code below:

tidy_sample_topics <- sample_topics %>%
unnest_tokens(word, originaltext)


and I've gotten a data frame similar to below:

It yielded more than a million records.

If I'm on the right path, I've tried nesting the data frame by word.trend column similar to below:

tidy_sample_topics <- tidy_sample_topics %>%
group_by(word.trend) %>%
mutate(linenumber = row_number()) %>%
nest()


Now, how do I create a function to pass along to the LDA(topic_dtm, k=3) function and perform topic modeling all at once.

I'm fairly new to functions and map() in purrr package so it would be great if someone can help me with a code that would somehow perform topic modeling (in this example by word.trend column, e.g., 3 topics for money, bank, stock, price based on my current unnested data frame all at once. Then perform mutate using dplyr to add the beta column result for each of the word found in each word.trend group.