Java Mailing List Archive

http://www.r-help.com/

Home » Home (12/2007) » R Help for Statistical Computing »

[R] Row median of Date class variables in a data frame

Stephen D. Weigand

2005-02-23

Replies:

I am trying to calculate the median of each row of a
data frame where the data frame consist of
columns of class Date.

Below are my test data and best attempt at using apply.
I didn't see a solution via Google or the Baron search
site.

I'd be grateful for any suggestions or solutions.
I'm using R 2.0.0 on Mac OS X.

Thank you,

Stephen Weigand


### Test data

date1 <- c(1000, 2000, 3000,4000)
date2 <- date1 + 100
date3 <- date2 + 100

class(date1) <- class(date2) <- class(date3) <- "Date"

test <- data.frame(date1, date2, date3)

print(test)

### create a function for apply()
medDate <- function(x){
 obj <- unclass(unlist(x))
 med <- median(obj, na.rm = TRUE)
 med
 class(med) <- "Date"
 med
}

medDate(test$date1) # works
medDate(test[1,])  # works

apply(test, 1, medDate) # gives error: 'need numeric data'
apply(test, 2, medDate) # gives error: 'need numeric data'

______________________________________________
R-help@(protected)
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
©2008 r-help.com - Jax Systems, LLC, U.S.A.