shannonpileggi / SR--Oshita--iFixit

Undergraduate summer research project partnering with iFixit
1 stars 0 forks source link

Having problems with regex function #6

Closed lisaoshita closed 7 years ago

lisaoshita commented 7 years ago

I’m practicing with product classification, and I wanted to search through the device variable and classify phones in one category. But the cases aren’t consistent, some are uppercase and some are lowercase, so I wanted to use the regex function to allow for case-insensitive matching. But calling regex in r doesn’t seem to work for me. I’ve copied and pasted the code used in the Data camp lesson to r and that doesn’t output the same either- it doesn’t recognize any patterns. I can’t figure this one out

This is the code that I copied and pasted from Data Camp. R doesn't match any patterns with this x <- c("Cat", "CAT", "cAt") str_view(x, regex("cat", ignore_case = TRUE), match = TRUE)

shannonpileggi commented 7 years ago

So the str_view function allows you to see the results of what matches in the right hand side in an html viewer. So try this code and notice what is gray and what is white. The argument for match was changed to NA so you can see what does and does not match.

x <- c("Cat", "CAT", "cAt", "dog", "gerbil") str_view(x, regex("cat", ignore_case = TRUE), match = NA)

If you want the matching results as an object that you can work with, try using str_detect instead.

str_detect(x, regex("cat", ignore_case = TRUE))

lisaoshita commented 7 years ago

Hmm, I'm still getting the same output and str_detect returns all false

anthonypileggi commented 7 years ago

Maybe you need to load the stringr package first? Or, try updating R and the stringr package.

Here's what I get: screenshot 2017-07-04 12 11 47

And changing the option to match = NA: screenshot 2017-07-04 12 12 42

lisaoshita commented 7 years ago

I ended up reinstalling all of the packages and it worked. Thanks!