[R] Searching for keyword values in a text (configuration) file
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@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.
[R] Searching for keyword values in a text (configuration) file
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@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.
Re: [R] Searching for keyword values in a text (configuration) file
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 __ R-help@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. __ R-help@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.
Re: [R] Searching for keyword values in a text (configuration) file
Something like this? library(Hmisc) t - readLines(clipboard) t [1] DEVICE = 'PC' CPU_SPEED = '1999', '233' ix - grep(CPU_SPEED,t) loc - substring.location(t[ix],,) cpu - substring(t[ix],loc$first+2) cpu [1] '233' On 27/09/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 __ R-help@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. -- = David Barron Said Business School University of Oxford Park End Street Oxford OX1 1HP __ R-help@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.
Re: [R] Searching for keyword values in a text (configuration) file
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 __ R-help@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. __ R-help@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.
Re: [R] Searching for keyword values in a text (configuration) file
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 [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 __ R-help@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. __ R-help@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.