如何读取带有多个标题的文件?

时间:2015-04-17 11:20:33

标签: r input io read.table

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

我的文件负责人:

>\>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变量(如dataframe)?

或者,如果有人可以帮助我们删除标题并添加另一个表示表格数量的列(或者显示此行是另一个表的第一行),那么它很好。

我不想将其作为字符串阅读并解析它

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

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

3 个答案:

答案 0 :(得分:2)

如果不将整个内容作为字符串读取并解析它,实际上并不容易,但您可以轻松地将此类操作转换为函数,就像我在{{read.mtable函数中所做的那样。 3}}

此处它适用于您的样本数据:

## 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

正如您所看到的,它创建了一个list个11 data.frame,每个都以“Len =”值命名。

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

答案 1 :(得分:1)

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

# 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"

答案 2 :(得分:0)

最后,我用几行代码解析数据并将数据导入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

感谢其他人的有用答案和评论,这些答案和评论可以帮助我写出答案:)