Personally, I prefer "one" liners. Something along the lines:
my.df <- data.frame(col1 = sample(c(1,2), 10, replace = TRUE),
col2 = as.factor(sample(10)), col3 = letters[1:10],
col4 = sample(c(TRUE, FALSE), 10, replace = TRUE))
my.list <- list(list1 = my.df, list2 = my.df[3], list3 = letters)
The data structure should mimic the idea of writer's problem, and not the exact verbatim structure. I really appreciate it when variables don't overwrite my own variables or god forbid, functions (like
df).
Alternatively, one could cut a few corners and point to a pre-existing data set, something like:
library(vegan)
data(varespec)
ord <- metaMDS(varespec)
Don't forget to mention any special packages you might be using.
If you're trying to demonstrate something on larger objects, you can try
my.df2 <- data.frame(a = sample(10e6), b = sample(letters, 10e6, replace = TRUE))
If you're working with spatial data via the
raster package, you can generate some random data. A lot of examples can be found in the package vignette, but here's a small nugget.library(raster)
r1 <- r2 <- r3 <- raster(nrow=10, ncol=10)
values(r1) <- runif(ncell(r1))
values(r2) <- runif(ncell(r2))
values(r3) <- runif(ncell(r3))
s <- stack(r1, r2, r3)
If you're in need of some spatial object as implemented in
sp, you can get some data sets via external files (like ESRI shape file) in "spatial" packages (see Spatial view in Task Views).library(rgdal)
ogrDrivers()
dsn <- system.file("vectors", package = "rgdal")[1]
ogrListLayers(dsn)
ogrInfo(dsn=dsn, layer="cities")
cities <- readOGR(dsn=dsn, layer="cities")
############################################################## Disclaimer: The Best answers from stackoverflow.com has been listed here.
No comments:
Post a Comment