错误:“参数不是原子向量;它不是原子向量.coercing [1] FALSE". [英] Error: "argument is not an atomic vector; coercing[1] FALSE"
问题描述
我是R的新手,遇到了麻烦(1)概括了我的情况的先前堆栈溢出答案,以及(2)了解R文档.所以我转向这个社区,希望有人会引导我通过.
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.
我有以下代码,其中 data1
是文本文件:
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))
我看到的错误消息是:
argument is not an atomic vector; coercing[1] FALSE
当我使用is.vector()确认数据类型时,看起来应该没问题:
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
我用于str_detect函数的参考是 https://www.rdocumentation.org/packages/stringr/versions/1.4.0/topics/str_detect .
Reference I used for str_detect function is https://www.rdocumentation.org/packages/stringr/versions/1.4.0/topics/str_detect.
这是 data1
的输出-我试图将第四行与最后一行发生错误!"相匹配:
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
经过一些初步测试,看来问题出在我如何打开 data1
,而不一定是 str_detect()代码>.
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`
问题::因此,我确信问题在于(1)我使用了错误的函数,或者(2)我为此文件类型错误地加载了文件.我以前从未在R中使用过文本文件,所以我有点迷路.
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!
推荐答案
我认为这是因为 read.delim
将文本文件作为数据帧而不是向量在读取这就是 str_detect
所需要的.
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!")
之所以有效,是因为data1现在是1列的数据帧. data2 [,1]
返回该数据帧的第一列(也是唯一列)的所有行,并将其作为向量返回.
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.
但是!这里的问题是您正在使用 read.delim
,它用于分隔的文本文件(例如,带有分隔符(例如,')的csv文件),而您的数据却没有.更好的方法是使用 readlines
函数,该函数将为您返回字符向量.
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)
然后 str_detect
应该起作用.
str_detect(data1, "An Error Has Occurred!")
这篇关于错误:“参数不是原子向量;它不是原子向量.coercing [1] FALSE".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!