[R] data.frame to list

Gabor Grothendieck ggrothendieck at gmail.com
Wed Apr 5 03:56:15 CEST 2006


Try this:
dd <- read.table(myfile, as.is = TRUE)
lst <- as.list(dd[,2])
names(lst) <- dd[,1]
lst$title

For a data frame (but not for a matrix) the names are the same
as the colnames and dimnames is a two-element list
consisting of the rownames and colnames.  Try this:

DF <- data.frame(A = 1:5, B = 11:15)
rownames(DF) <- letters[1:5]
names(DF)
colnames(DF)
rownames(DF)
dimnames(DF)
DF[c("a", "b"),]
DF[,"B"]
DF$B


On 4/4/06, Larry Howe <linux at comjet.com> wrote:
> Hi,
>
> I'm new to R, and I'm not a statistician (stunned silence). I am trying to do
> the following:
>
> 1. read in a 2-column data file, e.g.
>
> status  new
> db      green
> title   "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 know I could do this by coding
>
> lst = list(title="Most Significant Excursions")
>
> however I would like to populate the list at runtime from a data file. I have
> tried
>
> df = read.delim("params.txt", as.is=T)
>
> and that gives me a data frame with the data in it. It seems to be a 2-element
> list where the first element is the first column of data and the second
> element is the second column of data. Not what I want.
>
> Maybe this is a related question: what's the relation between names, dimnames,
> row.name, and rownames? Is there any way to select records out of a data
> frame or vector based on any of these attributes?
>
> Thanks,
> Larry Howe
>
> ______________________________________________
> R-help at 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
>




More information about the R-help mailing list