Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a key-value file in R

Tags:

file

r

Is there a way to read a simple text key-value file in R ...

Key1=Value1
Key2=Value2
Key3=Value3

Ideally I want to access the data like this:

myfile$Key1 should return Value1 , myfile@Key2 should return Value2
and so on

Cheers! MadSeb

P.S I looked into the stashR and the filehash packages and while these packages implement nice key value databases, they don't store the database in simple/readable text format.

like image 977
MadSeb Avatar asked Oct 29 '25 10:10

MadSeb


1 Answers

What about something like this:

dframe <- read.table(file='yourfile.txt',header=FALSE,
                     sep='=',col.names=c('Key','Value'))

then, if you want a faster access by key, you can use data.table e.g. :

library(data.table)
dframe <- read.table(file='yourfile.txt',header=FALSE,
                     sep='=',col.names=c('Key','Value'))
dtable <- data.table(dtfrm,key='Key')

dtable['Key1']
like image 117
digEmAll Avatar answered Oct 31 '25 02:10

digEmAll



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!