R语言 seq函数

在一个文档中定位到第一个空行,读取其后的所有文本
get.msg <- function(path)
{

con <- file(path, open = "rt", encoding = "latin1")

text <- readLines(con)

# The message always begins after the first full line break
msg <- text[seq(which(text == "")[1]+1, length(text), 1)]
close(con)
return(paste(msg, collapse = "\n"))
}
报错:
Error in seq.default(which(text == "")[1]+1, length(text), 1) :
'from' cannot be NA, NaN or infinite
示例文档:
From [email protected] Thu Aug 22 13:17:22 2002
Return-Path: <[email protected]>
Delivered-To: [email protected]
Received: from localhost (localhost [127.0.0.1])
by phobos.labs.spamassassin.taint.org (Postfix) with ESMTP id 136B943C32
for <zzzz@localhost>; Thu, 22 Aug 2002 08:17:21 -0400 (EDT)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Dwindows-1252" http-equiv=3DContent-T=
ype>

seq函数是R语言中的基本函数,其功能是生成一个向量。
使用方法如下:
seq(0,1,length.out=100)
上一行语句生成一个100个值的等差数列,首项为0,末项为1
拓展:
你还可以使用rep函数生成向量。
用法如下:
rep(0,100)
第一个位置表示重复的单元,第二个位置表示重复的次数。这样会生成一个100个0的向量。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-03-30

在一个文档中定位到第一个空行,读取其后的所有文本  get.msg <- function(path)  {      con <- file(path, open = "rt", encoding = "latin1")        text <- readLines(con)      # The message always begins after the first full line break    msg <-... 文档定位第空行读取其所文本

get.msg <- function(path)

{

con <- file(path, open = "rt", encoding = "latin1")

text <- readLines(con)

# The message always begins after the first full line break

msg <- text[seq(which(text == "")[1]+1, length(text), 1)]

close(con)

return(paste(msg, collapse = "\n"))

}

报错:

Error in seq.default(which(text == "")[1]+1, length(text), 1) : 

'from' cannot be NA, NaN or infinite 展开 示例文档:

From [email protected]  Thu Aug 22 13:17:22 2002

Return-Path: <[email protected]>

Delivered-To: [email protected].org

Received: from localhost (localhost [127.0.0.1])

by phobos.labs.spamassassin.taint.org (Postfix) with ESMTP id 136B943C32

for <zzzz@localhost>; Thu, 22 Aug 2002 08:17:21 -0400 (EDT)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML><HEAD>

<META content=3D"text/html; charset=3Dwindows-1252" http-equiv=3DContent-T=

ype>

本回答被网友采纳
第2个回答  2015-06-28
seq函数是产生序列用的他的用法是seq(from,to,by)或者是seq(下界,by=,length=)
下面是用r运行的结果
seq(2,6,2)
[1] 2 4 6
seq(10,by=2,length=5)
[1] 10 12 14 16 18
追问:
我知道seq函数的用法。我只是想问,这里为什么会报错
追答:
你的from是空的,你需要设定一下from,我觉得你用which(text == "")[1]+1作为from不对吧,你这样写不懂什么意思
追问:
需求是从那段文本中定位第一个空行,那么text==“”,该写什么呢?无论我写什么,都会报错。把==改成!=就不报错,但是结果还是不对
追答:
但是你的文本里有 多个空格, 这样表述感觉不正确
追问:
[1]不是表示第一个么?
第3个回答  2019-06-25
兄弟,你是不是在看《机器学习:实用案例解析》这本书。这个问题我也遇到了。把这一行:con <- file(path, open = "rt", encoding = "latin1")中的encoding删掉,就解决了。(改成‘utf-8’也不行)就是字符编码的问题。
seq(which(text == "")[1]+1, length(text), 1)函数报'from' cannot be infinite,seq方法即向量生成,必须给它一个起点元素。因为which()方法从text字符向量中找空字符串元素找不到。问题出在text中。readLines方法在读到错误编码后会中断,导致text中可能还没有读到第一个空行。所以上面也报in readLines:incomplete final line found(最终行不完整)
ps:我也是找了一圈seq,readLines的文档,最后把控制台warning的文件单条拎出来file、readLines之后print出来,才发现问题出在file方法这儿。
第4个回答  推荐于2017-09-05
seq函数是产生序列用的他的用法是seq(from,to,by)或者是seq(下界,by=,length=)
下面是用r运行的结果
seq(2,6,2)
[1] 2 4 6
seq(10,by=2,length=5)
[1] 10 12 14 16 18追问

我知道seq函数的用法。我只是想问,这里为什么会报错

追答

你的from是空的,你需要设定一下from,我觉得你用which(text == "")[1]+1作为from不对吧,你这样写不懂什么意思

追问

需求是从那段文本中定位第一个空行,那么text==“”,该写什么呢?无论我写什么,都会报错。把==改成!=就不报错,但是结果还是不对

追答

但是你的文本里有 多个空格, 这样表述感觉不正确

追问

[1]不是表示第一个么?

本回答被提问者采纳