【问题标题】:How can i read a file with multi headers?如何读取具有多个标题的文件?
【发布时间】:2015-06-24 05:18:31
【问题描述】:

我有一个包含多个标题的文件,我也需要这些标题。

我的文件头:

>\>1 Len = 254

>13 112 1 18

>15 112 1 30

>22 11  3 25

>\>1 Reverse  Len = 254

>14 11 1 15

>\>2 Len = 186

>19 15 2 34

>25 11  3 25

>....

如何读取此文件,并将值导入 R 变量(如数据框)?

或者,如果有人可以帮助我们删除标题并添加代表表格数量的另一列(或显示该行是另一个表格的第一行),那就太好了

我不想把它读成字符串并解析它

如果有帮助,数据是来自 MUMMER 包的报告

我还在这里上传了一个示例: http://m.uploadedit.com/ba3c/1429271308686.txt

【问题讨论】:

  • 通常的方法是使用readLines 加载文件,然后根据需要将每一行转换为字符或数字。稍微搜索一下,你会发现几个和你类似的问题。
  • 您能否上传/链接到实际数据文件的缩短版本(.txt、.dat...),以便我们尝试一下?
  • 基本上你必须编写自己的解析器,如果还没有人为这种文件格式写过。
  • “我不想将其读取为字符串并对其进行解析” ...欢迎来到数据科学/统计学中 80% 的时间/工作。
  • library(SOfun); read.mtable("http://m.uploadedit.com/ba3c/1429271308686.txt", ">")?但这本质上是将文件作为字符串读取并解析它。 Here's "SOfun".

标签: r input io read.table


【解决方案1】:

如果不将整个内容作为字符串读取并解析,确实很难做到这一点,但是您可以轻松地将此类操作转换为函数,就像我在 my "SOfun" package 中使用 read.mtable 函数所做的那样。

这里应用于您的示例数据:

## library(devtools)
## install_github("mrdwab/SOfun")

library(SOfun)
X <- read.mtable("http://m.uploadedit.com/ba3c/1429271308686.txt", ">")
X <- X[!grepl("Reverse", names(X))]

names(X)
#  [1] "> 1  Len = 354"   "> 2  Len = 127"   "> 3  Len = 109"   "> 4  Len = 52"   
#  [5] "> 5  Len = 1189"  "> 6  Len = 1007"  "> 7  Len = 918"   "> 10  Len = 192" 
#  [9] "> 11  Len = 169"  "> 13  Len = 248"  "> 14  Len = 2500"
X[1]
# $`> 1  Len = 354`
#        V1  V2  V3  V4
# 1  203757   1   1  35
# 2  122132   1   1  87
# 3  203756   1   1 354
# 4       1   1   1 354
# 5   42364  12   1  89
# 6  203757  37  37  91
# 7  122132  90  90  38
# 8   42364 102  91  37
# 9  203757 129 129 168
# 10  42364 140 129 212
# 11 122132 129 129 212
# 12 203757 298 298  43

如您所见,它创建了一个由 11 个data.frames 组成的list,每个都以“Len =”值命名。

这里使用的两个参数是文件位置(这里是 URL)和chunkID,可以设置为您想要匹配的正则表达式或固定模式。在这里,我们希望匹配以“>”开头的任何行,以指示新数据集的开始位置。

【讨论】:

  • 文件长度或其他方面是否有任何限制?我为我的示例文件应用该函数,它是我文件的一小部分,它可以正常工作,但是当我将它用于原始文件时它出错并说:扫描错误(文件,什么,nmax,sep,dec,quote , skip, nlines, na.strings, : 第 2 行没有 6 个元素
  • @ameerosein,不应该有文件长度限制。您提到的错误与read.table 无法正确猜测您的列有关。空格是唯一的分隔符吗?
  • 是的...正如您在消息中看到的那样,它是关于第二行...我的示例的第二行和原始文件是相同的...我复制了起始行作为示例..所以唯一的区别是文件的大小......
【解决方案2】:

或者如果你想要一个冗长繁琐的方法......

# if you just want the data and not the header information

x<-read.table("1429271308686.txt",comment.char=">")

# in case all else fails, my somewhat cumbersome solution...
x<-scan("1429271308686.txt",what="raw")

# extract the lengths, ind1 has all the lengths
ind1<-x=="="
ind1<-c(ind1[length(ind1)],ind1[-length(ind1)]) # take the value that comes after "="
cumsum(ind1)
lengths<-as.numeric(x[ind1])[c(TRUE,FALSE)] # only want one of the lengths

# remove the unwanted characters
ind2<-x==">"
ind2<-c(ind2[length(ind2)],ind2[-length(ind2)]) # take the value that comes after ">"

ind3<-x==">"|x=="Len"|x=="="|x=="Reverse"
dat<-as.numeric(x[!(ind1|ind2|ind3)]) # remove the unwanted

# arrange as matrix
mat<-matrix(dat,length(dat)/4,4,byrow=T)

# the number of rows for each block
block<-(c(1:length(x))[duplicated(cumsum(!ind2))][c(FALSE,TRUE)]-c(1:length(x))[duplicated(cumsum(!ind2))][c(TRUE,FALSE)]-5)/4

# the number for each block
id<-as.numeric(x[ind2])[c(TRUE,FALSE)]

# new vector
mat<-cbind(rep(id,block),mat) # note, this assumes that the last line is again "> Reverse"

【讨论】:

  • 谢谢,但有问题!我们还需要反向表,在我上传的示例中,所有反向表都是空的,但其中一些有记录!我更新了我的问题中的小例子,请看一下
  • 第二个问题是,如果任何 Reverse 表不为空,那么从那个地方到最后所有索引都会出错...
【解决方案3】:

最后我用几行代码解析数据并将数据导入R

我将所有表合并为一个表并添加一个新列来表示名称 表...

就是这样:

lns = readLines("filename.txt") ; # read the data as character

idx = grepl(">", lns) ; # location of all ">"s

df = read.table(text=lns[!idx]) ; # read all lines as table unless those who starts with ">"

wd = diff(c(which(idx), length(idx) + 1)) - 1  ; # finding the index of each table to add in new column

df$label = rep(lns[idx], wd) ; # add table indices in a new column

另一种处理这种特殊情况的方法是使用其他论坛中有人建议我的 perl onliner,我不知道它是什么但它有效:

https://support.bioconductor.org/p/66724/#66767

感谢其他人提供有用的答案和帮助我写出答案的 cmets :)

【讨论】:

    猜你喜欢
    • 2016-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多