Note that the argument to apply() is a matrix, not a data frame, so your
object has been coerced to a matrix. That is spelled out on the help page for apply(), as it is frequent misuse.


You would be better off using a matrix of class Date in the first place if you want row operations.


On Wed, 23 Feb 2005, Stephen D. Weigand wrote:

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'

-- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595

______________________________________________
[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

Reply via email to