0

I have a Data Frame of this format:

df <- data.frame(names= c('perform data cleansing','information categorisation'))

                      names
1     perform data cleansing
2 information categorisation

I am trying to obtain this format:

                      names             tokens
1      perform data cleansing           perform
1      perform data cleansing              data
1      perform data cleansing         cleansing
2  information categorisation       information
2  information categorisation    categorisation
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
spajeo
  • 31
  • 4

1 Answers1

2

I like tidyr::unnest for this:

library(dplyr)
library(tidyr)
df %>% mutate(tokens = strsplit(as.character(names), split = " ")) %>%
  unnest()
# names         tokens
# 1     perform data cleansing        perform
# 2     perform data cleansing           data
# 3     perform data cleansing      cleansing
# 4 information categorisation    information
# 5 information categorisation categorisation

But you can also do it all in base:

tokens = strsplit(as.character(df$names), split = " ")
result = data.frame(names = rep(df$names, lengths(tokens)),
                    tokens = unlist(tokens),
                    stringsAsFactors = FALSE)
#                        names         tokens
# 1     perform data cleansing        perform
# 2     perform data cleansing           data
# 3     perform data cleansing      cleansing
# 4 information categorisation    information
# 5 information categorisation categorisation

A version that comes with extra features for text analysis is tidytext::unnest_tokens:

df$names = as.character(df$names)
tidytext::unnest_tokens(df, output = tokens, input = names, drop = FALSE)
#                          names         tokens
# 1       perform data cleansing        perform
# 1.1     perform data cleansing           data
# 1.2     perform data cleansing      cleansing
# 2   information categorisation    information
# 2.1 information categorisation categorisation
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294