[R] Searching for keyword values in a text (configuration) file

Gabor Grothendieck ggrothendieck at gmail.com
Wed Sep 27 16:46:47 CEST 2006


Here is a slight simplification of this one using row.names= in
the read.table to avoid the subsequent manipulations.  We
use Lines0 defined in the earlier post:


# replace next line with something like Lines <- readLines("myfile.dat")
Lines <- readLines(textConnection(Lines0))

Lines <- gsub("=", ",", Lines)
Lines <- gsub(" ", "", Lines)

DF <- read.table(textConnection(Lines), row.names = 1, sep = ",", fill = TRUE,
       colClasses = "character", header = FALSE)

DF["CPU_SPEED", 2]


On 9/27/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> Read in data using readLines and replace
> = with comma and delete all spaces.
> Then reread using read.table and set the
> rownames to column 1 removing column 1.
>
> # test data
> Lines0 <- "DEVICE = 'PC'
> CPU_SPEED = '1999', '233'
> "
>
> # if reading from a file then
> # replace next line with something like Lines <- readLines("myfile.dat")
> Lines <- readLines(textConnection(Lines0))
> Lines <- gsub("=", ",", Lines)
> Lines <- gsub(" ", "", Lines)
> DF <- read.table(textConnection(Lines), sep = ",", fill = TRUE,
>        colClasses = "character", header = FALSE)
> rownames(DF) <- DF[,1]
> DF <- DF[,-1]
>
> DF["CPU_SPEED", 2]
>
>
>
>
>
> On 9/27/06, Andre Jung <ajung at gfz-potsdam.de> wrote:
> > Hi,
> >
> > I would like to read values from an ASCII text file that contains
> > information in the following format:
> >
> > DEVICE = 'PC'
> > CPU_SPEED = '1999', '233'
> > ...
> >
> > It's like a config file.
> >
> > How can I e.g. get R to read the 2nd value of CPU_SPEED?
> > How do I go through text files and search for keywords and their values?
> >
> > Thanks a lot,
> > Andre
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>



More information about the R-help mailing list