[R] embedding data frame in R code?
David Winsemius
dwinsemius at comcast.net
Fri Aug 3 03:27:34 CEST 2012
On Aug 2, 2012, at 5:57 PM, ivo welch wrote:
> I would like to insert a few modest size data frames directly into my
> R code. a short illustration example of what I want is
>
> d <- read.csv( _END_, row.names=1 )
> , "col1", "col2"
> "row1",1,2
> "row2",3,4
> __END__
>
> right now, the data sits in external files. I could put each column
> into its own vector and then combine into a data frame, but this seems
> ugly. is there a better way to embed data frames? I searched for the
> answer via google, but could not find it. it wasn't obvious in the
> data import/export guide.
It's not really
d <- read.csv( text=' "col1", "col2"
"row1",1,2
"row2",3,4'
, row.names=1 )
d
############
col1 col2
row1 1 2
row2 3 4
#############
dput(d)
##########
structure(list(col1 = c(1L, 3L), col2 = c(2L, 4L)), .Names = c("col1",
"col2"), class = "data.frame", row.names = c("row1", "row2"))
#
# So you can assign the 'structure' to a name and make a duplicate:
d2 <- structure(list(col1 = c(1L, 3L), col2 = c(2L, 4L)), .Names =
c("col1",
"col2"), class = "data.frame", row.names = c("row1", "row2"))
d2
#-----------
col1 col2
row1 1 2
row2 3 4
Besides dput, there is also the dump function. The import-export
manual does mention it, but not in a context which would be very
helpful. The source() function will run text through the parse-eval-
print loop.
source(textConnection(' d3 <- structure(list(col1 = c(1L, 3L), col2
= c(2L, 4L)), .Names = c("col1", "col2"), class = "data.frame",
row.names = c("row1", "row2"))' ) )
> d3
col1 col2
row1 1 2
row2 3 4
>
> regards,
>
> /iaw
> ----
> Ivo Welch (ivo.welch at gmail.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.
David Winsemius, MD
Alameda, CA, USA
More information about the R-help
mailing list