Here is one more solution using the same Lines0 from last time.
This one uses strapply from gsubfn to pick out all the fields in
each line creating a list named by the keywords (rather than
a data frame as in the previous solution). The names of the
components are the keywords so we remove them from
the contents of the list itself since we don't need them twice:
library(gsubfn)
# replace next line with something like Lines <- readLines("myfile.dat")
Lines <- readLines(textConnection(Lines0))
parms <- strapply(Lines, "[^ ',=]+", c, USE.NAMES = TRUE)
parms <- lapply(parms, "[", -1)
parms[["CPU_SPEED"]][2]
On 9/27/06, Gabor Grothendieck <[EMAIL PROTECTED]> 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 <[EMAIL PROTECTED]> 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
> >
> > ______________________________________________
> > [email protected] 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.
> >
>
______________________________________________
[email protected] 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.