Question on writing a function to look up a text string on a data.table
My data look like this:
word1 word2 frequency
1 thanks_for the 247
2 one_of the 177
3 if_you dont 168
4 all_the time 164
5 to_get to 156
6 to_see you 152
I wrote this code to find a match in the word1 column, then next output the Corresponding word2.
This with time dat1 is a large data frame of texts with 13974 rows. The 3 columns are word1, word2, and frequency.
Using the dplyr package and this code to look for word2 when I type in word1
dat1 %>% filter(word1 == "enjoying_case") %>% select(-word1)
The console will return :
word2 frequency
1 presentations 77
Which is what I want. However, when I write this in a function
nxtword1<- function(word1){ dat1 %>% filter(word1 == word1) %>% select(-word1)}
)}
typing in
nxtword1("enjoying_case")
the result is this chaos:
word2 frequency
1 1699
2 1656
3 1437
4 1257
5 1186
6 1174
7 1160
. . .
Try choosing a different name for your function argument, dplyr is evaluating word1 as "enjoying_case" and trying to exclude that as a column instead of the column word1.