Re: [R] How to convert a factor to a numeric?
Chris Bergstresser subtlety.com> writes: : : Date: Tue, 01 Mar 2005 16:45:36 -0600 : From: Chris Bergstresser <[EMAIL PROTECTED]> : To: : Subject: [R] How to convert a factor to a numeric? : : : Hi all -- : : I've got two columns, both of which correspond to three factor : levels (e.g., column1 is "a", "b", or "c"; column2 is "x", "y", or "z"). : I'd like to generate a third column, consisting on whether the two : factors are correctly aligned for a given case (in this example, "a" : corresponds to "x", "b" to "y", and "c" to "z"). For example: : : a x TRUE : a y FALSE : b y TRUE : c z TRUE : b x FALSE : : Several questions: : : The easiest way seemed to me to be comparing the numeric values : across columns, but the encodings are (a=1, b=2, c=3) and (x=1, y=3, : z=2). Is there a way to change the underlying value representing each : factor, so I could just run an equality on them? If f1 and f2 are the two factors: as.numeric(f1) == as.numeric(factor(as.character(f2))) : Is there a simple way to check for correspondence without recoding : the factors? I am not sure I would recommend this but it could be done like this: as.numeric(f1) == ifelse(f2=="x", 1, 5-as.numeric(f2)) : In the help for factor(), it says "In particular, 'as.numeric' : applied to a factor is meaningless, and may happen by implicit coercion. : To "revert" a factor 'f' to its original numeric values, : 'as.numeric(levels(f))[f]' is recommended and slightly more efficient : than 'as.numeric(as.character(f))'." However, I get the following : results. What's going on? I suspect they were thinking of the case where the levels themselves are of class numeric as in factor(c(10,10,11,11,12,12)) since it does not seem to be correct otherwise. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] How to convert a factor to a numeric?
# If fact1 and fact2 are your factors, let prm be the permutation such that # levels(fact2) corresponds to ("aligns to") levels(fact1)[prm] . In your example, the permutation is # apparently the identity, (1:3). #Then levels(fact2)[prm[fact1]]==fact2 ## does what you want. I wouldn't be surprised if there are cleverer solutions, though. -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box > -Original Message- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf Of Chris > Bergstresser > Sent: Tuesday, March 01, 2005 2:46 PM > To: r-help@stat.math.ethz.ch > Subject: [R] How to convert a factor to a numeric? > > Hi all -- > > I've got two columns, both of which correspond to three factor > levels (e.g., column1 is "a", "b", or "c"; column2 is "x", > "y", or "z"). > I'd like to generate a third column, consisting on whether the two > factors are correctly aligned for a given case (in this example, "a" > corresponds to "x", "b" to "y", and "c" to "z"). For example: > > a x TRUE > a y FALSE > b y TRUE > c z TRUE > b x FALSE > > Several questions: > > The easiest way seemed to me to be comparing the numeric values > across columns, but the encodings are (a=1, b=2, c=3) and (x=1, y=3, > z=2). Is there a way to change the underlying value > representing each > factor, so I could just run an equality on them? > Is there a simple way to check for correspondence without > recoding > the factors? > In the help for factor(), it says "In particular, 'as.numeric' > applied to a factor is meaningless, and may happen by > implicit coercion. > To "revert" a factor 'f' to its original numeric values, > 'as.numeric(levels(f))[f]' is recommended and slightly more efficient > than 'as.numeric(as.character(f))'." However, I get the following > results. What's going on? > > > f = gl(3, 1, 6, labels=c("a", "b", "c")) > > f > [1] a b c a b c > Levels: a b c > > as.numeric(levels(f))[f] > [1] NA NA NA NA NA NA > Warning message: > NAs introduced by coercion > > as.numeric(as.character(f)) > [1] NA NA NA NA NA NA > Warning message: > NAs introduced by coercion > > as.numeric(f) > [1] 1 2 3 1 2 3 > > __ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] How to convert a factor to a numeric?
Hi all -- I've got two columns, both of which correspond to three factor levels (e.g., column1 is "a", "b", or "c"; column2 is "x", "y", or "z"). I'd like to generate a third column, consisting on whether the two factors are correctly aligned for a given case (in this example, "a" corresponds to "x", "b" to "y", and "c" to "z"). For example: a x TRUE a y FALSE b y TRUE c z TRUE b x FALSE Several questions: The easiest way seemed to me to be comparing the numeric values across columns, but the encodings are (a=1, b=2, c=3) and (x=1, y=3, z=2). Is there a way to change the underlying value representing each factor, so I could just run an equality on them? Is there a simple way to check for correspondence without recoding the factors? In the help for factor(), it says "In particular, 'as.numeric' applied to a factor is meaningless, and may happen by implicit coercion. To "revert" a factor 'f' to its original numeric values, 'as.numeric(levels(f))[f]' is recommended and slightly more efficient than 'as.numeric(as.character(f))'." However, I get the following results. What's going on? > f = gl(3, 1, 6, labels=c("a", "b", "c")) > f [1] a b c a b c Levels: a b c > as.numeric(levels(f))[f] [1] NA NA NA NA NA NA Warning message: NAs introduced by coercion > as.numeric(as.character(f)) [1] NA NA NA NA NA NA Warning message: NAs introduced by coercion > as.numeric(f) [1] 1 2 3 1 2 3 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html