R只读取特定的文本行,并将它们存储在data.frame中

时间:2013-09-03 23:34:09

标签: r sed dataframe

我有以下问题。有一个我要导入R的文本文件。 它看起来像这样:

#  Below, those lines that start with a minus sign and an l (-l)
#  define loci, while those that start with a minus sign and an i (-i)
#  define epistatic interactions between the loci previously defined.
#  The loci must be defined before the interactions can be defined.
#  For some experimental designs, some interactions will be ignored.
#   
-start model
#
# Here is the data on the QTLS...
#
-t     3   is the number of traits
#
#
-k     1
# for trait -number     1
#
#      #  ..Chrom..Markr.    .RecombiL.    .RecombiR.    .Additive.    .Dominance
-l     1      2     19         0.0823        0.0028        1.1712        0.0000
#
#   QTL1   QTL2   Type       Value  
#
#
#
-k     1
# for trait -number     2
#
#      #  ..Chrom..Markr.    .RecombiL.    .RecombiR.    .Additive.    .Dominance
-l     1      2     14         0.0309        0.0564        0.4635        0.0000
#
#   QTL1   QTL2   Type       Value  
#
#
#
-k     1
# for trait -number     3
#
#      #  ..Chrom..Markr.    .RecombiL.    .RecombiR.    .Additive.    .Dominance
-l     1     15     61         0.0782        0.0261        0.1200        0.0000
#
#   QTL1   QTL2   Type       Value  
#
#
#
#End of this block of information
#
-end model
#

现在我想读取以-l开头的行并将它们保存在data.frame中。 有人知道实现这个目标的方法吗?谢谢。

1 个答案:

答案 0 :(得分:3)

datL <- readLines(textConnection("#  -start model
#
# Here is the data on the QTLS...
#
-t     3   is the number of traits
#
#
-k     1
# for trait -number     1
#
#      #  ..Chrom..Markr.    .RecombiL.    .RecombiR.    .Additive.    .Dominance
-l     1      2     19         0.0823        0.0028        1.1712        0.0000
#
#   QTL1   QTL2   Type       Value  
#
#
#
-k     1
# for trait -number     2
#
#      #  ..Chrom..Markr.    .RecombiL.    .RecombiR.    .Additive.    .Dominance
-l     1      2     14         0.0309        0.0564        0.4635        0.0000
#
#   QTL1   QTL2   Type       Value  
#
#
#
-k     1
# for trait -number     3
#
#      #  ..Chrom..Markr.    .RecombiL.    .RecombiR.    .Additive.    .Dominance
-l     1     15     61         0.0782        0.0261        0.1200        0.0000
#
#   QTL1   QTL2   Type       Value  
#
#
#
#End of this block of information
#
-end model
#"))

现在代码:

> goodL <- grepl("^.l", datL)
> datL[goodL]
[1] "-l     1      2     19         0.0823        0.0028        1.1712        0.0000"
[2] "-l     1      2     14         0.0309        0.0564        0.4635        0.0000"
[3] "-l     1     15     61         0.0782        0.0261        0.1200        0.0000"


> read.table(text=datL[goodL])[ ,-1]
  V2 V3 V4     V5     V6     V7 V8
1  1  2 19 0.0823 0.0028 1.1712  0
2  1  2 14 0.0309 0.0564 0.4635  0
3  1 15 61 0.0782 0.0261 0.1200  0

两个或三个技巧正在构建一个逻辑索引,只选择在第二个位置具有'l'的行,然后将“好东西”作为一个字符向量传递给read.table并删除“-l”的无关列。