Displaying 20 results from an estimated 20000 matches similar to: "switch and factors"
2008 Jul 25
3
melting a list: basic question
Dear list,
I'm trying to use the reshape package to perform a merging operation
on a list of data.frames as illustrated below,
> a <- 1:10
> example <- list( data.frame(a=a, b=sin(a)), data.frame(a=a,
> b=cos(a)) )
>
> melt(example, id = a)
this produces the desired result, where the data.frames have been
coerced into one with a common identifier variable
2009 Oct 08
2
proto and get()
Dear all,
In mucking around with ggplot2, I've hit the following snag,
library(ggplot2)
# this returns a grob, OK
GeomAbline$icon()
# lines[GRID.lines.9]
# this returns the function icon, OK
GeomAbline$icon
# proto method (instantiated with ): function (.)
# linesGrob(c(0, 1), c(0.2, 0.8))
# <environment: 0x13e6800>
Now I want to wrap this in a function,
getIcon <-
2008 Dec 02
1
legend idea for latticeExtra
Dear list,
I've written a small utility function to add arbitrary legend(s) to a
lattice graph (or a combination of them), much like the legend
function of base graphics. I though perhaps it could be useful to
someone else, or improved by suggestions. I understand this goes
against the lattice paradigm somewhat, in that you short-cut the link
between group variables and the
2009 Sep 28
2
dichromat, regexp, and grid objects
Dear list,
The dichromat package defines a dichromat function which "Collapses
red-green color distinctions to approximate the effect of the two
common forms of red-green colour blindness, protanopia and
deuteranopia."
library(dichromat)
library(grid)
colorStrip <-
function (colors = 1:3, draw = TRUE)
{
x <- seq(0, 1 - 1/length(colors), length = length(colors))
y <-
2009 Mar 25
2
"[.data.frame" and lapply
Dear all,
Trying to extract a few rows for each element of a list of
data.frames, I'm puzzled by the following behaviour,
> d <- lapply(1:4, function(i) data.frame(x=rnorm(5), y=rnorm(5)))
> str(d)
>
> lapply(d, "[", i= c(1)) # fine, this extracts the first columns
> lapply(d, "[", j= c(1, 3)) # doesn't do nothing ?!
>
> library(plyr)
2009 Mar 25
2
"[.data.frame" and lapply
Dear all,
Trying to extract a few rows for each element of a list of
data.frames, I'm puzzled by the following behaviour,
> d <- lapply(1:4, function(i) data.frame(x=rnorm(5), y=rnorm(5)))
> str(d)
>
> lapply(d, "[", i= c(1)) # fine, this extracts the first columns
> lapply(d, "[", j= c(1, 3)) # doesn't do nothing ?!
>
> library(plyr)
2010 Aug 27
2
export 4D data as povray density files
Dear list,
I wish to visualise some 4D data as a kind of colour / translucent
cloud in 3D. I haven't seen such plots in R (but perhaps I missed a
feature of rgl). The easiest option I found would be to export the
data in povray's df3 (density file) format and visualise it with
povray.
The format specification baffles me a little,
http://www.povray.org/documentation/view/3.6.1/374/ ;
2010 Mar 10
4
write.fortran
Dear all,
I'm trying to write tabular data to a text file, these data will be
the input of a Fortran program. The format needs to be
"(i7,2x,7(e15.7,2x))". I have not been able to find a clean way of
producing this output with write.table. I searched for a
"write.fortran" function similar to read.fortran() in package utils
but I couldn't find any. Below is a small
2010 May 18
1
substitute, expression and factors
Dear list,
I am puzzled by this,
substitute(expression(x), list(x = factor(letters[1:2])))
# expression(1:2)
Why do I get back the factor levels inside the expression and not the
labels? The following work as I expected,
substitute(expression(x), list(x = letters[1:2]))
# expression(c("a", "b"))
substitute(x, list(x = factor(letters[1:2])))
# [1] a b
# Levels: a b
2009 Apr 03
3
plyr and table question
Dear all,
I'm puzzled by the following example inspired by a recent question on
R-help,
cc <- textConnection("user_id website time
20 google 0930
21 yahoo 0935
20 facebook 1000
25 facebook 1015
61 google 0940")
d <- read.table(cc, head=T) ; close(cc)
table(d$user_id) # count the
2010 May 18
1
lattice::panel.levelplot.raster too picky with unequal spacing
Dear all,
I got a couple of warnings using panel.levelplot.raster,
In panel.levelplot.raster(..., interpolate = TRUE) :
'y' values are not equispaced; output will be wrong
although I was quite sure my data were equally spaced (indeed, I
created them with seq()). A closer look at the source code reveals
that the function tests for exact uniformity in grid spacing,
if
2009 Dec 19
4
expand.grid game
Dear list,
In a little numbers game, I've hit a performance snag and I'm not sure
how to code this in C.
The game is the following: how many 8-digit numbers have the sum of
their digits equal to 17?
The brute-force answer could be:
maxi <- 9 # digits from 0 to 9
N <- 5 # 8 is too large
test <- 17 # for example's sake
sum(rowSums(do.call(expand.grid, c(list(1:maxi),
2010 Sep 09
1
scalable < > delimiters in plotmath
Dear list,
I read in ?plotmath that I can use bgroup to draw scalable delimiters
such as [ ] and ( ). The same technique fails with < > however, and I
cannot find a workaround,
grid.text(expression(bgroup("<",atop(x,y),">")))
Error in bgroup("<", atop(x, y), ">") : invalid group delimiter
Regards,
baptiste
sessionInfo()
R version
2010 Sep 09
1
scalable < > delimiters in plotmath
Dear list,
I read in ?plotmath that I can use bgroup to draw scalable delimiters
such as [ ] and ( ). The same technique fails with < > however, and I
cannot find a workaround,
grid.text(expression(bgroup("<",atop(x,y),">")))
Error in bgroup("<", atop(x, y), ">") : invalid group delimiter
Regards,
baptiste
sessionInfo()
R version
2008 Aug 09
2
levels values of cut()
Dear list,
I have the following example, from which I am hoping to retrieve
numeric values of the factor levels (that is, without the brackets):
>
> x <- seq(1, 15, length=100)
> y <- sin(x)
>
> my.cuts <- cut(which(abs(y) < 1e-1), 3)
> levels(my.cuts)
hist() does not suit me for this, as it does not necessarily respect
the number of breaks.
getAnywhere
2008 Dec 10
4
tapply within a data.frame: a simpler alternative?
Dear list,
I have a data.frame with x, y values and a 3-level factor "group",
say. I want to create a new column in this data.frame with the values
of y scaled to 1 by group. Perhaps the example below describes it best:
> x <- seq(0, 10, len=100)
> my.df <- data.frame(x = rep(x, 3), y=c(3*sin(x), 2*cos(x),
> cos(2*x)), # note how the y values have a different
2010 Sep 21
3
bivariate vector numerical integration with infinite range
Dear list,
I'm seeking some advice regarding a particular numerical integration I
wish to perform.
The integrand f takes two real arguments x and y and returns a vector
of constant length N. The range of integration is [0, infty) for x and
[a,b] (finite) for y. Since the integrand has values in R^N I did not
find a built-in function to perform numerical quadrature, so I wrote
my own after
2011 Apr 26
2
grid stringHeight
Dear all,
I'm puzzled by the behavior of stringHeight in the grid package.
Consider the following test,
library(grid)
test <- function(lab="dog", ...){
g1 <- textGrob(lab)
g2 <- rectGrob(height=grobHeight(g1), width=grobWidth(g1))
gg <- gTree(children=gList(g1,g2), ...)
print(c("height:", convertUnit(stringHeight(lab), "mm",
2010 Oct 31
3
extracting named vector from dataframe
Suppose df is a dataframe with one named row of numeric observations. I want
to coerce df into a named vector.
as.vector does not work as I expected: as.vector(df) returns the original
dataframe, while as.vector(df,mode="numeric") returns an unnamed vector of
NAs.
This works:
> v <- as.numeric(as.matrix(df)); names(v) <- names(df);
I just wanted check if there
2010 Apr 17
2
grid.cap() requires more time?
Dear all,
I am puzzled by the following behavior of the new grid.cap() function,
which appears to run out of time when capturing the output of a
graphic. It works fine if I introduce a Sys.sleep(1) before executing
more code,
library(grid)
quartz()
grid.circle(gp=gpar(fill="black"))
gg <- grid.cap()
dev.new()
grid.raster(gg) ## completely blank
gg[gg!="white"] ## indeed