1

我有以下问题。我想将一个文本文件导入 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 中。有人知道实现这一目标的方法吗?谢谢。

4

1 回答 1

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”列s。

于 2013-09-04T00:16:52.573 回答