[R] how as.numeric() !-> factor

Tord Snall tord.snall at ebc.uu.se
Thu Jul 31 10:03:25 CEST 2003


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!




More information about the R-help mailing list