我试图从文本文件中提取表格,并在这里找到了几个较早的帖子,这些帖子解决了类似的问题。然而,似乎没有人能够有效解决我的问题。最有用的答案,我发现是我在这里较早的一个问题:R: removing header, footer and sporadic column headings when reading csv file从文本文件中提取表格
一个例子虚拟文本文件包含:
>
>
> ###############################################################################
>
> # Display AICc Table for the models above
>
>
> collect.models(, adjust = FALSE)
model npar AICc DeltaAICc weight Deviance
13 P1 19 94 0.00 0.78 9
12 P2 21 94 2.64 0.20 9
10 P3 15 94 9.44 0.02 9
2 P4 11 94 619.26 0.00 9
>
>
> ###############################################################################
>
> # the three lines below count the number of errors in the code above
>
> cat("ERROR COUNT:", .error.count, "\n")
ERROR COUNT: 0
> options(error = old.error.fun)
> rm(.error.count, old.error.fun, new.error.fun)
>
> ##########
>
>
我写了下面的代码以提取所需的表:
my.data <- readLines('c:/users/mmiller21/simple R programs/dummy.log')
top <- '> collect.models\\(, adjust = FALSE)'
bottom <- '> # the three lines below count the number of errors in the code above'
my.data <- my.data[-c(grep(bottom, my.data):length(my.data))]
my.data <- my.data[-c(1:grep(top, my.data))]
my.data <- my.data[c(1:(length(my.data)-4))]
aa <- as.data.frame(my.data)
aa
write.table(my.data, 'c:/users/mmiller21/simple R programs/dummy.log.extraction.txt', quote=F, col.names=F, row.name=F)
my.data2 <- read.table('c:/users/mmiller21/simple R programs/dummy.log.extraction.txt', header = TRUE, row.names = c(1))
my.data2
model npar AICc DeltaAICc weight Deviance
13 P1 19 94 0.00 0.78 9
12 P2 21 94 2.64 0.20 9
10 P3 15 94 9.44 0.02 9
2 P4 11 94 619.26 0.00 9
我宁愿避免不得不写,然后阅读my.data
以获得所需的数据帧。在此之前,步骤当前的代码返回my.data
字符串矢量:
[1] " model npar AICc DeltaAICc weight Deviance" "13 P1 19 94 0.00 0.78 9"
[3] "12 P2 21 94 2.64 0.20 9" "10 P3 15 94 9.44 0.02 9"
[5] "2 P4 11 94 619.26 0.00 9"
有一些方法可以让我的琴弦上述载体转化成这样的一个数据帧中dummy.log.extraction.txt
没有写,然后读my.data
?
行:
aa <- as.data.frame(my.data)
返回以下,它看起来像什么,我想:
# my.data
# 1 model npar AICc DeltaAICc weight Deviance
# 2 13 P1 19 94 0.00 0.78 9
# 3 12 P2 21 94 2.64 0.20 9
# 4 10 P3 15 94 9.44 0.02 9
# 5 2 P4 11 94 619.26 0.00 9
但是:
dim(aa)
# [1] 5 1
如果我可以拆分aa
成列然后我认为我会得到我想要的,而不必写,然后阅读my.data
。
我找到帖子:Extracting Data from Text Files但是,在发布的答案中,问题表似乎有固定的行数。在我的情况下,行数可以在1和20之间变化。另外,我宁愿使用base R
。在我的情况下,我认为bottom
和表的最后一行之间的行数是一个常数(这里是4)。
我也发现帖子:How to extract data from a text file using R or PowerShell?然而,在我的情况下,列的宽度不固定,我不知道如何拆分字符串(或行),所以只有七列。
鉴于上述所有可能我的问题是真的如何将对象aa
分成列。感谢您的任何建议或协助。
编辑:
实际日志由一台超级计算机产生并含有高达90000线。但是,日志中的行数差别很大。这就是为什么我使用top
和bottom
。
您的数据看起来像R对话输出控制台。人们想知道为什么表没有被导出,或者为什么你不能运行R代码来获得它。 – Roland
R文件在超级计算机上运行,表格取自该机器返回的日志。我不知道如何让超级计算机为我输出一张桌子。 –