Matthew Lundberg
2013-Apr-01 17:48 UTC
[R] Factor to numeric conversion - as.numeric(as.character(f))[f] - Language definition seems to say to not use this.
These two seem to be at odds. Is this the case?>From help(factor) - section Warning:To transform a factor f to approximately its original numeric values, as.numeric(levels(f))[f] is recommended and slightly more efficient than as.numeric(as.character(f)).>From the language definition - section 2.3.1:Factors are currently implemented using an integer array to specify the actual levels and a second array of names that are mapped to the integers. Rather unfortunately users often make use of the implementation in order to make some calculations easier. This, however, is an implementation issue and is not guaranteed to hold in all implementations of R. [[alternative HTML version deleted]]
Ista Zahn
2013-Apr-01 18:43 UTC
[R] Factor to numeric conversion - as.numeric(as.character(f))[f] - Language definition seems to say to not use this.
Hi Mathew, In what way are they at odds? On Mon, Apr 1, 2013 at 1:48 PM, Matthew Lundberg <matthew.k.lundberg at gmail.com> wrote:> These two seem to be at odds. Is this the case? > > >From help(factor) - section Warning: > > To transform a factor f to approximately its original numeric values, > as.numeric(levels(f))[f] is recommended and slightly more efficient than > as.numeric(as.character(f)). > > >From the language definition - section 2.3.1: > > Factors are currently implemented using an integer array to specify the > actual levels and > a second array of names that are mapped to the integers. Rather > unfortunately users often > make use of the implementation in order to make some calculations easier. > This, however, > is an implementation issue and is not guaranteed to hold in all > implementations of R. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Peter Ehlers
2013-Apr-01 18:49 UTC
[R] Factor to numeric conversion - as.numeric(as.character(f))[f] - Language definition seems to say to not use this.
On 2013-04-01 10:48, Matthew Lundberg wrote:> These two seem to be at odds. Is this the case? > >>From help(factor) - section Warning: > > To transform a factor f to approximately its original numeric values, > as.numeric(levels(f))[f] is recommended and slightly more efficient than > as.numeric(as.character(f)). > >>From the language definition - section 2.3.1: > > Factors are currently implemented using an integer array to specify the > actual levels and > a second array of names that are mapped to the integers. Rather > unfortunately users often > make use of the implementation in order to make some calculations easier. > This, however, > is an implementation issue and is not guaranteed to hold in all > implementations of R.Hint: f <- factor(sample(5, 10, TRUE)) as.numeric(levels(f))[f] g <- factor(sample(letters[1:5], 10, TRUE)) as.numeric(levels(g))[g] Peter Ehlers> > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Reasonably Related Threads
- Factor to numeric conversion - as.numeric(levels(f))[f] - Language definition seems to say to not use this.
- control the conversion of factor to numeric
- why doesn't as.character of this factor create a vector of characters?
- character to numeric conversion
- any way to convert back to DateTime class when "accidental" conversion to numeric?