:45 +0100
>From: r-help-boun...@r-project.org (on behalf of nfdi...@gmail.com (Ernest
>Adrogué i Calveras))
>Subject: [R] sapply puzzlement
>To: r-help@r-project.org
>
>Hi,
>
>I have this data.frame with two variables in it,
>
>> z
> V1 V2
>1 10 8
>2 NA
In addition to what has already been suggested you could use ..
mapply(function(x,y) x-y, z,means)
which returns
V1 V2
[1,] 0.333 -2.7142857
[2,] NA 7.2857143
[3,] -0.667 -3.7142857
[4,] -6.667 NA
[5,] NA -0.7142857
[6,] 1.333
On Jan 27, 2011, at 7:16 PM, Ernest Adrogué i Calveras wrote:
Hi,
I have this data.frame with two variables in it,
z
V1 V2
1 10 8
2 NA 18
3 9 7
4 3 NA
5 NA 10
6 11 12
7 13 9
8 12 11
and a vector of means,
means <- apply(z, 2, function (col) mean(na.omit(col)))
means
V1
sapply(z, function(row) ...) does not actually grab a row at a time out of
'z'. It grabs a column (because 'z' is a data.frame)
You may want:
t(apply(z, 1, function(row) row - means))
or:
t(t(z) - means)
Hope that helps,
-David Johnston
--
View this message in context:
http://r.789695.n4.n
Hi,
I have this data.frame with two variables in it,
> z
V1 V2
1 10 8
2 NA 18
3 9 7
4 3 NA
5 NA 10
6 11 12
7 13 9
8 12 11
and a vector of means,
> means <- apply(z, 2, function (col) mean(na.omit(col)))
> means
V1V2
9.67 10.714286
My intention was substracting mean
5 matches
Mail list logo