如何从文本文件中读取信息?

Gee*_*ata 6 text-processing r logfile-analysis text-files

我有数百个文本文件,每个文件中包含以下信息:

*****Auto-Corelation Results******
1     .09    -.19     .18     non-Significant

*****STATISTICS FOR MANN-KENDELL TEST******
S=  609
VAR(S)=      162409.70
Z=           1.51
Random : No trend at 95%

*****SENs STATISTICS ******
SEN SLOPE =  .24
Run Code Online (Sandbox Code Playgroud)

现在,我想读取所有这些文件,并从每个文件(例如)中"收集" Sen的统计信息,.24并将其与相应的文件名一起编译成一个文件.我必须在R里做.

我使用过CSV文件,但不知道如何使用文本文件.

这是我现在使用的代码:

require(gtools)
GG <- grep("*.txt", list.files(), value = TRUE)
GG<-mixedsort(GG)
S <- sapply(seq(GG), function(i){
X <- readLines(GG[i])
grep("SEN SLOPE", X, value = TRUE)
})
spl <- unlist(strsplit(S, ".*[^.0-9]"))
SenStat <- as.numeric(spl[nzchar(spl)])
SenStat<-data.frame( SenStat,file = GG)
write.table(SenStat, "sen.csv",sep = ", ",row.names = FALSE)
Run Code Online (Sandbox Code Playgroud)

当前代码无法正确读取所有值并给出此错误:

Warning message:
NAs introduced by coercion 
Run Code Online (Sandbox Code Playgroud)

另外,我没有将文件名作为Output的另一列.请帮忙!


诊断1

代码也在读取=符号.这是print(spl)的输出

 [1] ""       "5.55"   ""       "-.18"   ""       "3.08"   ""       "3.05"   ""       "1.19"   ""       "-.32"  
[13] ""       ".22"    ""       "-.22"   ""       ".65"    ""       "1.64"   ""       "2.68"   ""       ".10"   
[25] ""       ".42"    ""       "-.44"   ""       ".49"    ""       "1.44"   ""       "=-1.07" ""       ".38"   
[37] ""       ".14"    ""       "=-2.33" ""       "4.76"   ""       ".45"    ""       ".02"    ""       "-.11"  
[49] ""       "=-2.64" ""       "-.63"   ""       "=-3.44" ""       "2.77"   ""       "2.35"   ""       "6.29"  
[61] ""       "1.20"   ""       "=-1.80" ""       "-.63"   ""       "5.83"   ""       "6.33"   ""       "5.42"  
[73] ""       ".72"    ""       "-.57"   ""       "3.52"   ""       "=-2.44" ""       "3.92"   ""       "1.99"  
[85] ""       ".77"    ""       "3.01"
Run Code Online (Sandbox Code Playgroud)

诊断2

发现我认为的问题.负号有点棘手.在某些文件中

SEN SLOPE =-1.07
SEN SLOPE = -.11
Run Code Online (Sandbox Code Playgroud)

由于=之后的差距,我正在为第一个获得NAs,但是代码正在读取第二个.如何修改正则表达式来解决这个问题?谢谢!

Ric*_*ven 10

假设"text.txt"是您的一个文本文件.读入R with readLines,可以grep用来查找包含的行SEN SLOPE.如果没有其他参数,则grep返回找到正则表达式的元素的索引号.在这里,我们发现它是第11行.添加value = TRUE参数以获取读取的行.

x <- readLines("text.txt")
grep("SEN SLOPE", x)
## [1] 11
( gg <- grep("SEN SLOPE", x, value = TRUE) )
## [1] "SEN SLOPE =  .24"
Run Code Online (Sandbox Code Playgroud)

要查找.txt工作目录中的所有文件,我们可以使用list.files正则表达式.

list.files(pattern = "*.txt")
## [1] "text.txt"
Run Code Online (Sandbox Code Playgroud)

循环播放多个文件

我创建了第二个文本文件,text2.txt其中包含不同的SEN SLOPE值,以说明如何将此方法应用于多个文件.我们可以使用sapply,然后使用strsplit,以获得spl所需的值.

GG <- list.files(pattern = "*.txt")
S <- sapply(seq_along(GG), function(i){
    X <- readLines(GG[i])
    ifelse(length(X) > 0, grep("SEN SLOPE", X, value = TRUE), NA)
    ## added 04/23/14 to account for empty files (as per comment)
})
spl <- unlist(strsplit(S, split = ".*((=|(\\s=))|(=\\s|\\s=\\s))"))
## above regex changed to capture up to and including "=" and 
## surrounding space, if any - 04/23/14 (as per comment)
SenStat <- as.numeric(spl[nzchar(spl)])
Run Code Online (Sandbox Code Playgroud)

然后我们可以将结果放入数据框并将其发送到文件中 write.table

( SenStatDf <- data.frame(SenStat, file = GG) )
##   SenStat      file
## 1    0.46 text2.txt
## 2    0.24  text.txt
Run Code Online (Sandbox Code Playgroud)

我们可以将它写入文件

write.table(SenStatDf, "myFile.csv", sep = ", ", row.names = FALSE)
Run Code Online (Sandbox Code Playgroud)

更新于2014年7月21日:

由于结果被写入文件,因此可以更加简单(和更快)

( SenStatDf <- cbind(
      SenSlope = c(lapply(GG, function(x){
          y <- readLines(x)
          z <- y[grepl("SEN SLOPE", y)]
          unlist(strsplit(z, split = ".*=\\s+"))[-1]
          }), recursive = TRUE),
      file = GG
 ) )
#      SenSlope file       
# [1,] ".46"   "test2.txt"
# [2,] ".24"   "test.txt" 
Run Code Online (Sandbox Code Playgroud)

然后写入和读入R

write.table(SenStatDf, "myFile.txt", row.names = FALSE)
read.table("myFile.txt", header = TRUE)
#   SenSlope      file
# 1     1.24 test2.txt
# 2     0.24  test.txt
Run Code Online (Sandbox Code Playgroud)