On Sun, 9 May 2004, Liaw, Andy wrote:
Dear R-help,
I've encounter what seems to me a strange problem with names-. Suppose I
define the function:
fun - function(x, f) {
m - tapply(x, f, mean)
ans - x - m[match(f, unique(f))]
names(ans) - names(x)
ans
}
which subtract
Remember tapply with a single factor in R returns a 1D array. What you
are seeing are the dimnames, not the names: look at attributes() on your
return value (or even name() or str() on it).
I suspect you intended an as.vector() call in the formation of m.
Brian
On Sun, 9 May 2004, Liaw, Andy
Liaw, Andy [EMAIL PROTECTED] writes:
[BTW, this is using the tip that Thomas Lumley posted about forming the
group means. I've wanted to write a `tsweep' function that's sort of the
cross of tapply() and sweep().]
Also notice that this is
unsplit(lapply(split(x, g), scale, scale=FALSE), g)
Dear R-help,
I've encounter what seems to me a strange problem with names-. Suppose I
define the function:
fun - function(x, f) {
m - tapply(x, f, mean)
ans - x - m[match(f, unique(f))]
names(ans) - names(x)
ans
}
which subtract out the means of `x' grouped by `f' (which is the
Execute these two commands:
ans - fun(x,f)
attributes(ans)
and you get this:
$dim
[1] 20
$dimnames
$dimnames[[1]]
[1] a a b c a d a b d d a b d c c c b c b
[20] d
so ans does not have names, it has dimnames. If you try dimnames(ans) - NULL
then its dimnames do get nulled