Let's say I have a document with some text, like this, from SO:
doc <- 'Questions with similar titles have frequently been downvoted and/or closed. Consider using a title that more accurately describes your question.'
I can then make a dataframe where every word has a row in a df:
library(stringi)
dfall <- data.frame(words = unlist(stri_extract_all_words(stri_trans_tolower(doc))))
We'll add a third column with its unique id. To get the ID, remove duplicates:
library(dplyr)
uniquedf <- distinct(data.frame(words = unlist(stri_extract_all_words(stri_trans_tolower(doc)))))
I'm struggling with how to match the rows against the two dataframes to extract the row index value from uniquedf
as a new row value for df
alldf <- alldf %>% mutate(id = which(uniquedf$words == words))
A dply method like this doesn't work.
Is there a more efficient way to do this?
To give an even simpler example to show the expected output, I'd like a dataframe that looks like this:
words id
1 to 1
2 row 2
3 zip 3
4 zip 3
Where my starting word vector is: doc <- c('to', 'row', 'zip', 'zip')
or doc <- c('to row zip zip')
. The id column adds a unique id for each unique word.