Extracting Data from Text Files

前端 未结 1 1198
囚心锁ツ
囚心锁ツ 2020-12-06 07:35

There appear to be similar questions to this in other languages but I can\'t find one in R.

I have a number of text files in the subdirectories of a directory; they

相关标签:
1条回答
  • 2020-12-06 07:44

    maybe

    library(stringr)
    f <- readLines("datafile.txt")
    cline <- grep("NUMBER OF CARTESIAN GAUSSIAN BASIS FUNCTIONS",f,
                        value=TRUE)
    val <- as.numeric(str_extract(cline,"[0-9]+$"))
    

    will work?

    To get the other values, try

    cline <- grep("^ +CPU timing information",f)
    (numstr <- sapply(str_extract_all(f[cline+2:5],"[0-9.]+"),as.numeric))
    ##         [,1]     [,2]     [,3]     [,4]
    ## [1,]    0.000    1.000    2.000    3.000
    ## [2,] 8853.469 8850.817 8851.925 8847.992
    ## [3,]  133.948  126.587  128.576  125.871
    ## [4,] 8987.417 8977.405 8980.501 8973.864
    

    The sapply has transposed the matrix of values, so the last row is the bit we want (corresponds to the last column in the file). Extract it using numstr[4,] or numstr[nrow(numstr),] or tail(numstr,1).

    (edit: allow spaces before the "CPU timing" string) (edit: do it right!)

    (To do this for all the log files, package it in a function and use list.files(pattern="\\.log$") in combination with sapply ...)

    0 讨论(0)
提交回复
热议问题