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

Tony Plate tplate at blackmesacapital.com
Thu Jul 31 16:48:57 CEST 2003


The problem is that the 2nd column in your data frame has been converted 
into a factor.  This happened because you used cbind() with mixed character 
and numeric vectors.  cbind() with these types of arguments will construct 
a character matrix.  Then when you passed that character matrix to 
as.data.frame() it converted both columns to factors.

Here's a simpler example of what happened:

 > cbind(letters[1:2], c(1,3))
      [,1] [,2]
[1,] "a"  "1"
[2,] "b"  "3"
 > x <- as.data.frame(cbind(letters[1:2], c(1,3)))
 > x
   V1 V2
1  a  1
2  b  3
 > as.numeric(x[,2])
[1] 1 2
 > as.numeric(as.character(x[,2]))
[1] 1 3
 >

With the data frame as you constructed it, you need an expression like 
round(as.numeric(as.character(Np.occup97.98[,2])), 2) to accomplish what 
you want.  It would probably be better to construct a more felicitous data 
frame in the first place:

 > df <- data.frame(site = levels(sums$site), Np.occup97.98 = 
sums$Ant.Nptrad97.98/Ant.trad$Ant.trad97.98)

(unless of course you had some unstated reason for constructing the data 
frame the way you did)

-- Tony Plate

At Thursday 10:03 AM 7/31/2003 +0200, 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




More information about the R-help mailing list