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

From: Michael Kubovy <kubovy_at_virginia.edu>
Date: Fri 19 Jan 2007 - 18:46:01 GMT

On Jan 19, 2007, at 12:54 PM, Ben Fairbank wrote:

> 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
>
> I cannot, however, find a way, other than "for" looping,
> to use statistical functions, such as mean or sd, to
> compute the new column.

times <- data.frame(time1 = rnorm(6, 50, 20), time2 = rnorm(6, 40, 15),

     time3 = rnorm(6, 60, 25), time4 = rnorm(6, 55, 23))

times[4,1] <- NA
times[5, c(2, 4)] <- NA
times[6, 4] <- NA
times$totaltime <- apply(times, 1, sum, na.rm = T)
times$meantime <- apply(times, 1, mean, na.rm = T) times$sdtime <- apply(times, 1, sd, na.rm = T)

      time1 time2 time3 time4 totaltime meantime sdtime

1 28.84859 29.94037 92.11518 71.80472 222.70886 89.08354 71.11911
2 50.72260 39.02439 61.18364 31.63962 182.57024 73.02810 55.68944
3 11.75829 28.61262 72.37066 79.23817 191.97974 76.79189 62.99902
4       NA 27.23659 75.69952 38.19262 141.12872 70.56436 44.52787
5 31.05109       NA 52.41755       NA  83.46864 55.64576 21.52078
6 54.01291 52.48922 53.97689       NA 160.47902 80.23951 46.33038



_____________________________

Professor Michael Kubovy
University of Virginia
Department of Psychology
USPS:     P.O.Box 400400    Charlottesville, VA 22904-4400
Parcels:    Room 102        Gilmer Hall
         McCormick Road    Charlottesville, VA 22903
Office:    B011    +1-434-982-4729
Lab:        B019    +1-434-982-4751
Fax:        +1-434-982-4766

WWW: http://www.people.virginia.edu/~mk9y/

        [[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. Received on Sat Jan 20 05:50:21 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.