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

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Fri 19 Jan 2007 - 18:51:26 GMT

Try this using the builtin data set anscombe:

transform(anscombe, rowMeans = rowMeans(anscombe))

On 1/19/07, Ben Fairbank <BEN@ssanet.com> 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@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.
>



R-help@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. Received on Sat Jan 20 05:58:45 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 19 Jan 2007 - 19:31:33 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.