Skip to content
Prev 361353 / 398506 Next

Extract from a text file

Val
Thank you so much Jeff. It worked for this example.

When I read it from a file (c:\data\test.txt) it did not work

KLEM="c:\data"
KR=paste(KLEM,"\test.txt",sep="")
indta <- readLines(KR, skip=46)  # not interested in the first 46 lines)

pattern <- "^.*group (\\d+)[^:]*: *([-+0-9.eE]*).*$"
firstlines <- grep( pattern, indta )
# Replace the matched portion (entire string) with the first capture # string
v1 <- as.numeric( sub( pattern, "\\1", indta[ firstlines ] ) )
# Replace the matched portion (entire string) with the second capture # string
v2 <- as.numeric( sub( pattern, "\\2", indta[ firstlines ] ) )
# Convert the lines just after the first lines to numeric
v3 <- as.numeric( indta[ firstlines + 1 ] )
# put it all into a data frame
result <- data.frame( Group = v1, Mean = v2, SE = v3 )

result
[1] Group Mean  SE
<0 rows> (or 0-length row.names)

Thank you in advance


On Tue, May 31, 2016 at 1:12 AM, Jeff Newmiller
<jdnewmil at dcn.davis.ca.us> wrote: