[R] how as.numeric() !-> factor
Prof Brian Ripley
ripley at stats.ox.ac.uk
Thu Jul 31 10:06:55 CEST 2003
You would have been helped by looking in the FAQ: please do so now.
Also, ?factor tells you not to use as.numeric.
On Thu, 31 Jul 2003, Tord Snall wrote:
> Dear all,
>
> I have divided two vectors:
>
> Np.occup97.98<- as.data.frame(cbind(site = levels(sums$site),
> Np.occup97.98 = sums$Ant.Nptrad97.98/Ant.trad$Ant.trad97.98))
>
> > Np.occup97.98
> site Np.occup97.98
> 1 erken97 0.342592592592593
> 2 erken98 0.333333333333333
> 3 rormyran 0.48471615720524
> 4 valkror 0.286026200873362
>
> However, at a later stage of the analysis I want
> > round(Np.occup97.98[,2], 2)
> Error in Math.factor(x, digits) : "round" not meaningful for factors
>
> neither did this work:
>
> > round(Np.occup97.98[,2], 2)
> Error in Math.factor(x, digits) : "round" not meaningful for factors
>
> or this:
>
> > round(as.numeric(Np.occup97.98[,2]), 2)
> [1] 3 2 4 1
> >
>
> because, as clearly written in the help file:
> "as.numeric for factors yields the codes underlying the factor levels, not
> the numeric representation of the labels."
>
> I've discovered this solution:
>
> > Np.occup97.98<- as.data.frame(cbind(site = levels(sums$site),
> + Np.occup97.98 =
> round(sums$Ant.Nptrad97.98/Ant.trad$Ant.trad97.98,2)))
> >
> > Np.occup97.98
> site Np.occup97.98
> 1 erken97 0.34
> 2 erken98 0.33
> 3 rormyran 0.48
> 4 valkror 0.29
>
>
> However, I would like to do this rounding later.
>
> Could someone give a tip. I think that I would have been helped by a
> sentence in help(as.numeric).
>
>
> Thanks in advance.
>
>
> Sincerely,
> Tord
>
>
>
>
> -----------------------------------------------------------------------
> Tord Snäll
> Avd. f växtekologi, Evolutionsbiologiskt centrum, Uppsala universitet
> Dept. of Plant Ecology, Evolutionary Biology Centre, Uppsala University
> Villavägen 14
> SE-752 36 Uppsala, Sweden
> Tel: 018-471 28 82 (int +46 18 471 28 82) (work)
> Tel: 018-25 71 33 (int +46 18 25 71 33) (home)
> Fax: 018-55 34 19 (int +46 18 55 34 19) (work)
> E-mail: Tord.Snall at ebc.uu.se
> Check this: http://www.vaxtbio.uu.se/resfold/snall.htm!
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>
>
--
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