R只读文本文件的特定行,并将其存储在data.frame中
我有以下问题。有一个文本文件,我想导入到R。 看起来是这样的:R只读文本文件的特定行,并将其存储在data.frame中,r,sed,dataframe,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
# 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中。
有人知道实现这一目标的方法吗?谢谢 datL goodL datL[goodL]
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
#"))
[1] “-l 12 19 0.0823 0.0028 1.1712 0.0000”
[2] “-l 12 14 0.0309 0.0564 0.4635 0.0000”
[3] “-l 11561 0.0782 0.0261 0.1200 0.0000”
>read.table(text=datL[goodL])[,-1]
V2 V3 V4 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”的无关列。,虽然我确信这在R中是可能的,您可能会发现使用sed之类的工具的解决方案要好得多。扩展查询的一个简单方法是在问题中添加sed标记。太棒了。非常感谢你。是否可以直接将文件导入textConnection?我使用了textConnection('text')
,它返回一个read.table
可以用作文件对象的“连接”,但您可能会使用file.path
函数替换一个带引号的有效路径和文件名,或者构建一个有效文件来引用驱动器上的文本文件。您可以将readLines(…)
与文件参数一起使用。我认为您可以使用file()调用来建立对象,但使用readLines()会更容易。
> 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