[R] data.frame to list
Brahm, David
David.Brahm at geodecapital.com
Wed Apr 5 20:32:04 CEST 2006
Larry Howe <linux at comjet.com> wants to:
> 1. read in a 2-column data file, e.g.
> status <tab> new
> db <tab> green
> title <tab> "Most Significant Excursions"
> 2. end up with an R list such that I can write e.g.
> lst$title
> and have R return "Most Significant Excursions".
I call this "reading a hash table" (because it consists of name/value
pairs), and here's a function to do it. Note this function allows you
to input a hash if you wish, and then override its values with the
data file. The argument defaults assume a tab-delimited text file
with a header row, which is ignored.
read.hash <- function(file, defaults=list(),
header=TRUE, sep="\t", ...) {
pl <- read.table(file, as.is=TRUE, header=header, sep=sep, ...)
for (i in seq(pl[[1]])) defaults[[pl[[1]][i]]] <- pl[[2]][i]
defaults
}
Also see the built-in "read.dcf", which uses a different input format,
but might suit your purpose.
-- David Brahm (brahm at alum.mit.edu)
More information about the R-help
mailing list