Skip to content

Newbie question: Statistical functions (e.g., mean, sd) in a "transform" statement?

5 messages · Ben Fairbank, Charles C. Berry, Michael Kubovy +2 more

#
Ben,

transform() is probably the wrong tool if what you want is to

 	'apply a function'

to the corresponding elements of time1, time2, ... , and return a vector 
of results.

If this is what you are after, the 'apply' family of functions is what you 
want.

See

 	?apply

and

 	?mapply

and the 'See Also's on each page.

Chuck Berry
On Fri, 19 Jan 2007, Ben Fairbank wrote:

            
Charles C. Berry                        (858) 534-2098
                                          Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	         UC San Diego
http://biostat.ucsd.edu/~cberry/         La Jolla, San Diego 92093-0901
#
Try this using the builtin data set anscombe:

transform(anscombe, rowMeans = rowMeans(anscombe))
On 1/19/07, Ben Fairbank <BEN at ssanet.com> wrote:
#
On Fri, 2007-01-19 at 11:54 -0600, Ben Fairbank wrote:
Here are two solutions, depending on whether you wanted the NA's or not,
and I assume you wanted the row means:
time1    time2     time3    time4 meantime
1 70.408543 48.92378  7.399605 95.93050 55.66561
2 17.231940 27.48530 82.962916 10.20619 34.47159
3 20.279220 10.33575 66.209290 30.71846 31.88568
4        NA 53.31993 12.398237 35.65782       NA
5  9.295965       NA 48.929201       NA       NA
6 63.966518 42.16304  1.777342       NA       NA
time1    time2     time3    time4 meantime
1 70.408543 48.92378  7.399605 95.93050 55.66561
2 17.231940 27.48530 82.962916 10.20619 34.47159
3 20.279220 10.33575 66.209290 30.71846 31.88568
4        NA 53.31993 12.398237 35.65782 33.79200
5  9.295965       NA 48.929201       NA 29.11258
6 63.966518 42.16304  1.777342       NA 35.96897

HTH

G