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]]
______________________________________________
[email protected] 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.