5

I'm new to R and am having trouble (1) generalizing previous stack overflow answers to my situation, and (2) understanding R documentation. So I turn to this community and hope someone will walk me through.


I have this code where data1 is a text file:

data1 <- read.delim(file.choose())
pattern <- c("An Error Has Occurred!")
str_detect(data1, regex(pattern, ignore_case = FALSE))

The error message I see is:

argument is not an atomic vector; coercing[1] FALSE

When I use is.vector() to confirm the data type, it looks like it should be fine:

is.vector(pattern)
#this returns [1] TRUE as the output

Reference I used for str_detect function is https://www.rdocumentation.org/packages/stringr/versions/1.4.0/topics/str_detect.


Edit 1: Here is the output of data1 - I'm trying to match the 4th to last line "An Error Has Occurred!":

Silk.Road.Forums
<fctr>
*
Welcome, Guest. Please login or register.
[ ] [ ] [Forever] [Login]
Login with username, password and session length
[ ] [Search]
â\200¢ Home
â\200¢ Search
â\200¢ Login
â\200¢ Register
â\200¢ Silk Road Forums
An Error Has Occurred!
The user whose profile you are trying to view does not exist.
Back
â\200¢ SMF | SMF © 2013, Simple Machines

Edit 2: After a bit of rudimentary testing, it looks like the issue is with how I opened up data1, not necessarily str_detect().

When I just create a vector, it works:

dataVector <- c("An Error Has Occurred!", "another one")
pattern <- c("An Error Has Occurred!")
str_detect(dataVector, pattern) # returns [1] TRUE FALSE

But when I try to use the function on the file, it doesn't

data1 <- read.delim(file.choose())
pattern <- c("An Error Has Occurred!")
str_detect(data1, pattern) # returns the atomic vector error message`

Problem: So I'm convinced that the problem is that (1) I'm using the wrong function or (2) I'm loading up the file wrong for this file type. I've never used text files in R before so I'm a bit lost.


That's all I have and thank you in advance for anyone willing to take a stab at helping!

bob
  • 117
  • 1
  • 1
  • 8
  • 1
    Can you post an example of your data? E.g. the output of `data1`. – Muon Apr 05 '20 at 01:42
  • Absolutely -- thanks for replying @Muon. I'll edit within the post to make it easier to read. (Edit: I added the output in the question) – bob Apr 05 '20 at 01:44
  • 1
    Thanks @bob. One more thing. Can you tell me the output of `class(data1)`? – Muon Apr 05 '20 at 01:57
  • @Moun, it comes out as `[1] "data.frame"` and I added more info in the question too – bob Apr 05 '20 at 02:03

2 Answers2

3

I think what is going on here is that read.delim is reading in your text file as a data frame and not a vector which is what str_detect requires.

For a quick work around you can try.

str_detect(data1[,1], "An Error Has Occurred!")

This works because right now data1 is a 1 column data frame. data2[,1] returns all rows for the first (and only) column of that data frame and returns it as a vector.

However! The problem here is you are using read.delim which is for delimited text files (i.e. like a csv file that has a separator such ',') which your data is not. Much better would be to use the function readlines which will return you a character vector.

# open a connection to your file
con <- file('path/to/file.txt',open="r")
# read file contents
data1 <- readLines(con)
# close the connection
close(con)

Then str_detect should work.

str_detect(data1, "An Error Has Occurred!")
Muon
  • 1,294
  • 1
  • 9
  • 31
  • 1
    No problem @bob! Best of luck with your project. R can have a steep learning curve. One thing to remember if you're having issues is to check that your data types are correct. The `class` function is very useful for this. – Muon Apr 05 '20 at 02:23
0

Just as.data.frame() your data, the the str_replace() works fine!

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 19 '22 at 03:35