[R] failure in simple calculation!!

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Mar 11 07:45:24 CET 2004


On Thu, 11 Mar 2004, Ko-Kang Kevin Wang wrote:

> Hi,
> 
> ----- Original Message ----- 
> From: "Mike Campana" <mike.campana at freesurf.ch>
> To: <r-help at stat.math.ethz.ch>
> Sent: Thursday, March 11, 2004 12:20 PM
> Subject: [R] failure in simple calculation!!
> 
> 
> > Hello
> >
> > I have a dataframe, at least I think I created it by using:
> >
> > DELTA <- as.data.frame(DELTA)
> >
> > The dataframe has 2 columns made of numbers("data" and "delta")
> >
> > If I use   DELTA$delta + 12
> > I get a sequence of NA  Warning message:
> > "+" not meaningful for factors in: Ops.factor(DELTA$deltatemp, 12)
> >
> > What does this mean? Can you explain me please why I can not make this
> > simple calculation? And what can I do to the calculation?
> >
> > further information:
> > mode(DELTA)
> > [1] "list"
> >
> > DELTA$delta
> > .....
> > [685] 6.04  7.84  8.01  8.43  6.6   6.33  8.42  8.86  10.25 8.2   9.32
> 9.77
> > [697] 7.9   6.42  5.95
> > 474 Levels: -0.02 -0.13 -0.22 -0.46 -0.55 -1.26 -1.83 -1.86 -2.45 -3.2 ...
> > 9.9
> > What does this last row mean?
> 
> 
> Several things.
> 
> 1) DELTA is not a data frame here.  It is a list: mode(DELTA)

A data frame is a list (almost all classed objects are), so the mode is
reported as a list.  Asking the class() would have been more revealing.

> 2) DELTA$delta is not a vector.  It is a factor.  For some reason when you
> generated DELTA, your delta has become a factor.  Arithematic operations on
> factors are meaningless.

as.data.frame will coerce character columns to factors.  The fix is 
probably

DELTA$delta <- as.numeric(as.character(DELTA$delta))

see the FAQ, Q7.12.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
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




More information about the R-help mailing list