hi: I searched the last 2 hours for a way to enter a data frame directly in my program. (I know how to read from a file.) that is, I would like to say something like d <- this.is.a.data.frame( c("obs1name", 0.2, 0.3), c("obs2name", 0.4, 1.0), c("obs3name", 0.6, 2.0) , varnames=c("name", "val1", "val2") ); everything I have tried sofar (usually, building with rbind and then names(d)) has come out with factors for the numbers, which is obviously not what I want. this must be a pretty elementary request, so it should probably be an example under data.frame (or read.table). of course, it is probably somewhere---just I have do not remember it and could not find it after 2 hours of searching. I also tried the r-help archives---at the very least, I hope we will get the answer there for future lookups. regards, /iaw
?data.frame says: Usage: data.frame(..., row.names = NULL, check.rows = FALSE, check.names TRUE) Arguments: ...: these arguments are of either the form 'value' or 'tag=value'. Component names are created based on the tag (if present) or the deparsed argument itself. which means you need to do the `transpose' of what you did: give data.frame() columns, rather than rows. E.g., dat <- data.frame(x=factor(c("A", "B", "A", "C"), y=1:4, rownames=LETTERS[1:4]) It's hard to build a data frame by row, because one needs to check and make sure data in each column are consistent, that data in a factor column have the right levels, etc. Andy> From: ivo welch > > hi: I searched the last 2 hours for a way to enter a data frame > directly in my program. (I know how to read from a file.) > that is, I > would like to say something like > > d <- this.is.a.data.frame( c("obs1name", 0.2, 0.3), > c("obs2name", 0.4, 1.0), > c("obs3name", 0.6, 2.0) , > varnames=c("name", "val1", "val2") ); > > everything I have tried sofar (usually, building with rbind and then > names(d)) has come out with factors for the numbers, which is > obviously > not what I want. this must be a pretty elementary request, so it > should probably be an example under data.frame (or read.table). of > course, it is probably somewhere---just I have do not remember it and > could not find it after 2 hours of searching. I also tried > the r-help > archives---at the very least, I hope we will get the answer there for > future lookups. > > regards, /iaw > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
easy to do it by column: > d <- data.frame(name=c("obs1name","obs2name","obs3name"),val1=c(0.2,0.4,0.6),val2=c(0.3,1.0,2.0),row.names=c("r1","r2","r3")) > d name val1 val2 r1 obs1name 0.2 0.3 r2 obs2name 0.4 1.0 r3 obs3name 0.6 2.0 > (when you do it by row, you get the numbers as factors because c("obs1name", 0.2, 0.3) etc. are character vectors) At Wednesday 01:29 PM 6/9/2004, ivo welch wrote:>hi: I searched the last 2 hours for a way to enter a data frame directly >in my program. (I know how to read from a file.) that is, I would like >to say something like > > d <- this.is.a.data.frame( c("obs1name", 0.2, 0.3), > c("obs2name", 0.4, 1.0), > c("obs3name", 0.6, 2.0) , > varnames=c("name", "val1", "val2") ); > >everything I have tried sofar (usually, building with rbind and then >names(d)) has come out with factors for the numbers, which is obviously >not what I want. this must be a pretty elementary request, so it should >probably be an example under data.frame (or read.table). of course, it is >probably somewhere---just I have do not remember it and could not find it >after 2 hours of searching. I also tried the r-help archives---at the >very least, I hope we will get the answer there for future lookups. > >regards, /iaw > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
thank you, chaps. ok, so this is not as straightforward as I had thought. perhaps the read.table() function should have the ability to read inline (terminated, e.g., by two newlines, or a usersettable string), rather than just from a file. this would be a nice feature. regards, /iaw
Hi Ivo! https://www.stat.math.ethz.ch/pipermail/r-help/2004-June/050601.html Sincerely Eryk *********** REPLY SEPARATOR *********** On 6/9/2004 at 3:29 PM ivo welch wrote:>>>hi: I searched the last 2 hours for a way to enter a data frame >>>directly in my program. (I know how to read from a file.) that is, I >>>would like to say something like >>> >>> d <- this.is.a.data.frame( c("obs1name", 0.2, 0.3), >>> c("obs2name", 0.4, 1.0), >>> c("obs3name", 0.6, 2.0) , >>>varnames=c("name", "val1", "val2") ); >>> >>>everything I have tried sofar (usually, building with rbind and then >>>names(d)) has come out with factors for the numbers, which is obviously >>>not what I want. this must be a pretty elementary request, so it >>>should probably be an example under data.frame (or read.table). of >>>course, it is probably somewhere---just I have do not remember it and >>>could not find it after 2 hours of searching. I also tried the r-help >>>archives---at the very least, I hope we will get the answer there for >>>future lookups. >>> >>>regards, /iaw >>> >>>______________________________________________ >>>R-help at stat.math.ethz.ch mailing list >>>https://www.stat.math.ethz.ch/mailman/listinfo/r-help >>>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.htmlDipl. bio-chem. Eryk Witold Wolski @ MPI-Moleculare Genetic Ihnestrasse 63-73 14195 Berlin 'v' tel: 0049-30-83875219 / \ mail: wolski at molgen.mpg.de ---W-W---- http://www.molgen.mpg.de/~wolski
ivo welch <ivo.welch <at> yale.edu> writes:> thank you, chaps. ok, so this is not as straightforward as I had > thought. perhaps the read.table() function should have the ability to > read inline (terminated, e.g., by two newlines, or a usersettable > string), rather than just from a file. this would be a nice feature.Have a look at the my.stdin function at: https://stat.ethz.ch/pipermail/r-help/2003-June/033622.html It is intended to be used in a sourced file as shown in the example there.