2013-07-04 149 views
0

我试图从文本文件中提取表格,并在这里找到了几个较早的帖子,这些帖子解决了类似的问题。然而,似乎没有人能够有效解决我的问题。最有用的答案,我发现是我在这里较早的一个问题: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线。但是,日志中的行数差别很大。这就是为什么我使用topbottom

+1

您的数据看起来像R对话输出控制台。人们想知道为什么表没有被导出,或者为什么你不能运行R代码来获得它。 – Roland

+0

R文件在超级计算机上运行,​​表格取自该机器返回的日志。我不知道如何让超级计算机为我输出一张桌子。 –

回答

3

可能是你真正的日志文件是完全不同的,更复杂,但是这一个,你可以使用read.table直接,你就必须用正确的参数发挥。

data <- read.table("c:/users/mmiller21/simple R programs/dummy.log", 
        comment.char = ">", 
        nrows = 4, 
        skip = 1, 
        header = TRUE, 
        row.names = 1) 

str(data) 
## 'data.frame': 4 obs. of 6 variables: 
## $ model : Factor w/ 4 levels "P1","P2","P3",..: 1 2 3 4 
## $ npar  : int 19 21 15 11 
## $ AICc  : int 94 94 94 94 
## $ DeltaAICc: num 0 2.64 9.44 619.26 
## $ weight : num 0.78 0.2 0.02 0 
## $ Deviance : int 9 9 9 9 

data 
## 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 
+0

谢谢。我应该提到日志文件包含大约20000行,这就是为什么我使用顶部和底部。但是,您的答案可能会有帮助。 –

3

read.table及其家人现在有一个选项,阅读文本:

> df <- read.table(text = paste(my.data, collapse = "\n")) 
> df 
    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 
> summary(df) 
model  npar   AICc  DeltaAICc   weight   Deviance 
P1:1 Min. :11.0 Min. :94 Min. : 0.00 Min. :0.000 Min. :9 
P2:1 1st Qu.:14.0 1st Qu.:94 1st Qu.: 1.98 1st Qu.:0.015 1st Qu.:9 
P3:1 Median :17.0 Median :94 Median : 6.04 Median :0.110 Median :9 
P4:1 Mean :16.5 Mean :94 Mean :157.84 Mean :0.250 Mean :9 
     3rd Qu.:19.5 3rd Qu.:94 3rd Qu.:161.90 3rd Qu.:0.345 3rd Qu.:9 
     Max. :21.0 Max. :94 Max. :619.26 Max. :0.780 Max. :9 
+0

谢谢。我应该提到日志文件包含大约20000行,这就是为什么我使用顶部和底部。但是,您的答案可能会有帮助。 –

1

这看起来很奇怪,你必须阅读的R控制台。无论如何,你可以使用这样一个事实,即你的表格行以数字开头,并使用诸如^[0-9]+之类的东西提取你的inetersting行。然后read.table就像@kohske所显示的那样。

readLines('c:/users/mmiller21/simple R programs/dummy.log') 
idx <- which(grepl('^[0-9]+',ll)) 
idx <- c(min(idx)-1,idx) ## header line 
read.table(text=ll[idx]) 
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 
+0

谢谢。我应该提到日志文件包含大约20000行,这就是为什么我使用顶部和底部。但是,您的答案可能会有帮助。 –

0

谢谢那些发布了答案的人。由于实际日志文件的大小,复杂性和可变性,我认为我需要继续使用变量topbottom。但是,我用dickoa的答案的元素来提出以下内容。

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))] 

x <- read.table(text=my.data, comment.char = ">") 
x 

# 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 <- 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[grep(top, my.data):grep(bottom, my.data)] 

x <- read.table(text=my.data, comment.char = ">") 
x