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
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 >
You might want to consider the use of 'attr' to assign attributes to an object. That way you can keep your object as a data.frame:> attr(x,"title") <- "Most Significant" > attr(x,"title")[1] "Most Significant" On 4/4/06, Larry Howe <linux@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@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 >-- Jim Holtman Cincinnati, OH +1 513 646 9390 (Cell) +1 513 247 0281 (Home) What the problem you are trying to solve? [[alternative HTML version deleted]]
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)