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

Charles C. Berry cberry at tajo.ucsd.edu
Fri Jan 19 19:36:33 CET 2007


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:

> Greetings listeRs -
>
>
>
> Given a data frame such as
>
>
>
> times
>
>       time1    time2     time3    time4
>
> 1  70.408543 48.92378  7.399605 95.93050
>
> 2  17.231940 27.48530 82.962916 10.20619
>
> 3  20.279220 10.33575 66.209290 30.71846
>
> 4         NA 53.31993 12.398237 35.65782
>
> 5   9.295965       NA 48.929201       NA
>
> 6  63.966518 42.16304  1.777342       NA
>
>
>
> one can use "transform" to total all or some columns, thus,
>
>
>
> times2 <- transform(times,totaltime=time1+time2+time3+time4)
>
>
>
>> times2
>
>       time1    time2     time3    time4 totaltime
>
> 1  70.408543 48.92378  7.399605 95.93050  222.6624
>
> 2  17.231940 27.48530 82.962916 10.20619  137.8863
>
> 3  20.279220 10.33575 66.209290 30.71846  127.5427
>
> 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
>
>
>
> I cannot, however, find a way, other than "for" looping,
>
> to use statistical functions, such as mean or sd, to
>
> compute the new column.  For example,
>
>
>
>>
> times2<-transform(times,meantime=(mean(c(time1,time2,time3,time4),na.rm=
> TRUE)))
>
>
>
>> times2
>
>
>
> time1    time2     time3    time4 meantime
>
> 1  70.408543 48.92378  7.399605 95.93050 45.54178
>
> 2  17.231940 27.48530 82.962916 10.20619 45.54178
>
> 3  20.279220 10.33575 66.209290 30.71846 45.54178
>
> 4         NA 53.31993 12.398237 35.65782 45.54178
>
> 5   9.295965       NA 48.929201       NA 45.54178
>
> 6  63.966518 42.16304  1.777342       NA 45.54178
>
>
>
> How can this be done?  And, generally, what is the recommended method
>
> for creating computed new columns in data frames when "for" loops take
>
> too long?
>
>
>
> With thanks for any suggestions,
>
>
>
> Ben Fairbank
>
>
>
> Using version 2.4.1 on a Windows XP professional operating system.
>
>
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

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



More information about the R-help mailing list