[R] Searching for keyword values in a text (configuration) file
ggrothendieck at gmail.com
Wed Sep 27 15:47:31 CEST 2006
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]
On 9/27/06, Andre Jung <ajung at gfz-potsdam.de> wrote:
> 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,
> R-help at stat.math.ethz.ch mailing list
> 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