Skip to content

reading in a (very simple) list from a file

6 messages · VA Smith, arun, David L Carlson

#
Apologies - I feel this is a very simple thing to do yet I am failing
massively. I keep finding information about how to do much more complicated
things (usually on this mailing list!), which then fail when I try to apply
it to my simple task.

Anyway, all I want to do is read in a series of key-value pairs from a file. 
I thought a list would be a good way to keep these, such that I could access
them like: listname$key

I was imagining a file like this:
key1 value1
key2 value2
key3 value3
...

(the keys will always be character strings, the values might be other types,
but they will always be single items)

I won't bore you with all the things I've tried. I'm sure I'm overlooking
something basic and simple, but I would greatly appreciate it if someone
could help me out here.

Thank you.

Best wishes,
Anne





--
View this message in context: http://r.789695.n4.nabble.com/reading-in-a-very-simple-list-from-a-file-tp4645741.html
Sent from the R help mailing list archive at Nabble.com.
#
Hi,
May be this:
set.seed(1)
?dat1<-data.frame(keys=paste0("key",1:5),value=sample(1:15,5,replace=TRUE))
list1<-lapply(split(dat1,dat1$keys),`[`,2)
list1$key2
#? value
#2???? 6

A.K.



----- Original Message -----
From: VA Smith <vas1 at st-andrews.ac.uk>
To: r-help at r-project.org
Cc: 
Sent: Wednesday, October 10, 2012 1:29 PM
Subject: [R] reading in a (very simple) list from a file

Apologies - I feel this is a very simple thing to do yet I am failing
massively. I keep finding information about how to do much more complicated
things (usually on this mailing list!), which then fail when I try to apply
it to my simple task.

Anyway, all I want to do is read in a series of key-value pairs from a file. 
I thought a list would be a good way to keep these, such that I could access
them like: listname$key

I was imagining a file like this:
key1 value1
key2 value2
key3 value3
...

(the keys will always be character strings, the values might be other types,
but they will always be single items)

I won't bore you with all the things I've tried. I'm sure I'm overlooking
something basic and simple, but I would greatly appreciate it if someone
could help me out here.

Thank you.

Best wishes,
Anne





--
View this message in context: http://r.789695.n4.nabble.com/reading-in-a-very-simple-list-from-a-file-tp4645741.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help at r-project.org 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.
#
HI,

By modifying the earlier solution using sapply()

set.seed(1)
?dat1<-data.frame(keys=paste0("key",1:5),value=sample(1:15,5,replace=TRUE))
list2<-sapply(split(dat1,dat1$keys),`[`,2)
?names(list2)<-dat1[,1]
list2$key2
#[1] 6
A.K.

----- Original Message -----
From: VA Smith <vas1 at st-andrews.ac.uk>
To: r-help at r-project.org
Cc: 
Sent: Wednesday, October 10, 2012 1:29 PM
Subject: [R] reading in a (very simple) list from a file

Apologies - I feel this is a very simple thing to do yet I am failing
massively. I keep finding information about how to do much more complicated
things (usually on this mailing list!), which then fail when I try to apply
it to my simple task.

Anyway, all I want to do is read in a series of key-value pairs from a file. 
I thought a list would be a good way to keep these, such that I could access
them like: listname$key

I was imagining a file like this:
key1 value1
key2 value2
key3 value3
...

(the keys will always be character strings, the values might be other types,
but they will always be single items)

I won't bore you with all the things I've tried. I'm sure I'm overlooking
something basic and simple, but I would greatly appreciate it if someone
could help me out here.

Thank you.

Best wishes,
Anne





--
View this message in context: http://r.789695.n4.nabble.com/reading-in-a-very-simple-list-from-a-file-tp4645741.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help at r-project.org 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.
#
It might help if you would bore us with at least one or two of the things
you have tried. It seems logical to read the file into a data frame using
read.table(). Then you can change it into any format you want:
Key1 1
Key2 2
Key3 3", header=TRUE)
key value
1 Key1     1
2 Key2     2
3 Key3     3

Just change text=". . . " to file="yourfilename". This will change the key
field into a factor. If you don't want to do that use
Key1 1
Key2 2
Key3 3", header=TRUE, stringsAsFactors=FALSE)

If you want to change the data.frame into a list, just use:
----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352
#
Brilliant!  Thank you both, this works!

Combined with the other suggestion of setting stringsAsFactors to FALSE when
reading in the data frame, I now have the behaviour I wanted.

I had been beginning to get the sense that one of the apply functions was
the solution.  I will now do some reading on split to understand precisely
what I'm doing...

Best wishes,
Anne






--
View this message in context: http://r.789695.n4.nabble.com/reading-in-a-very-simple-list-from-a-file-tp4645741p4645839.html
Sent from the R help mailing list archive at Nabble.com.
#
Hi again! Just in case someone ends up googling this for the same thing I
did, here is a modification to get around a little problem:

?dat1<-data.frame(keys=paste("key",5:1,sep=""),value=1:5)
splitlist <- split(dat1,dat1$keys)
list3<-sapply(splitlist,`[`,2) 
?names(list3)<-names(splitlist) 
list3$key2 
[1] 4

Because the list after split is stored in alphabetical order by name, it
reorders things, so you need to use that order when you name it.

Thanks again,
Anne




--
View this message in context: http://r.789695.n4.nabble.com/reading-in-a-very-simple-list-from-a-file-tp4645741p4645985.html
Sent from the R help mailing list archive at Nabble.com.