简体   繁体   English

错误:“参数不是原子向量;强制 [1] FALSE”

[英]Error: "argument is not an atomic vector; coercing[1] FALSE"

I'm new to R and am having trouble (1) generalizing previous stack overflow answers to my situation, and (2) understanding R documentation.我是 R 的新手,遇到问题 (1) 将以前的堆栈溢出答案概括为我的情况,以及 (2) 理解 R 文档。 So I turn to this community and hope someone will walk me through.所以我求助于这个社区,希望有人能引导我完成。


I have this code where data1 is a text file:我有这段代码,其中data1是一个文本文件:

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() 来确认数据类型时,看起来应该没问题:

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 .我用于 str_detect function 的参考是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:":编辑 1:这是data1的 output - 我试图将第 4 行与最后一行“发生错误:”相匹配:

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() .编辑 2:经过一些初步测试,问题似乎出在我打开data1的方式上,不一定是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但是当我尝试在文件上使用 function 时,它不会

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.问题:所以我确信问题是 (1) 我使用了错误的 function 或 (2) 我加载的文件对于这种文件类型是错误的。 I've never used text files in R before so I'm a bit lost.我以前从未在 R 中使用过文本文件,所以我有点迷路了。


That's all I have and thank you in advance for anyone willing to take a stab at helping!这就是我所拥有的,提前感谢任何愿意提供帮助的人!

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.我认为这里发生的事情是read.delim正在将您的文本文件作为数据框读取,而不是str_detect需要的向量。

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.这是有效的,因为现在 data1 是一个 1 列数据框。 data2[,1] returns all rows for the first (and only) column of that data frame and returns it as a vector. data2[,1]返回该数据帧的第一(也是唯一)列的所有行,并将其作为向量返回。

However!然而! The problem here is you are using read.delim which is for delimited text files (ie like a csv file that has a separator such ',') which your data is not.这里的问题是您使用的read.delim用于分隔文本文件(即,例如具有分隔符的 csv 文件),而您的数据不是。 Much better would be to use the function readlines which will return you a character vector.更好的是使用readlines读取行,它将返回一个字符向量。

# 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应该可以工作。

str_detect(data1, "An Error Has Occurred!")

Just as.data.frame() your data, the the str_replace() works fine!正如 .data.frame() 您的数据一样, str_replace() 工作正常!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 胁迫论证不是原子载体(无休止地重复) - coercing argument is not an atomic vector (repeating endlessly) 管道 stringr str_detect 到 str_extract 的问题 - 提取仅从第一行提取文本:参数不是原子向量; 胁迫 - Issue with piping stringr str_detect into str_extract - extract is only pulling text from 1st row: argument is not an atomic vector; coercing 绘制原子矢量时出错 - Error in plotting atomic vector (...) is not a length 1 atomic vector 错误 - (...) is not a length 1 atomic vector error as.vector(data) 中的错误:没有将此 S4 class 强制为向量的方法 - Error in as.vector(data) : no method for coercing this S4 class to a vector as.vector(x) 中的错误:没有将此 S4 class 强制为向量的方法 - Error in as.vector(x) : no method for coercing this S4 class to a vector “错误:参数 1 必须是数据帧或命名的原子向量。 ” 对于 `purrr::map_dfr()` - “Error: Argument 1 must be a data frame or a named atomic vector. ” for `purrr::map_dfr()` 将字符串强制转换为矢量 - Coercing String to Vector as.vector(data) 中的错误:无法将此 S4 class 强制转换为 R 中的向量 - Error in as.vector(data) : no method for coercing this S4 class to a vector in R CVXR:solve() 的问题 - as.vector(data) 中的错误:无法将此 s4 class 强制转换为向量 - CVXR: Problem with solve() - Error in as.vector(data): no method for coercing this s4 class to a vector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM