Hi everyone,
I understand the process of obtaining the internal integer codes for
the raw values of a factor (using as.numeric() as below), but what is
the best way to obtain these codes for the levels() of a factor (since
the as.numeric() results don't really make clear which code maps to
which level)?
fdata<-factor(c("b","b","c","a","b","c"),labels=c("I","II","III"))
fdata
levels(fdata)
as.numeric(fdata)
I thought something like this would make sense and work, but it throws an error:
as.numeric(levels(fdata))
Thanks!
Dan
Obtaining the internal integer codes of a factor XXXX
5 messages · Dan Abner, Bert Gunter, Duncan Murdoch +2 more
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130325/3706a9fb/attachment.pl>
On 25/03/2013 11:37 AM, Dan Abner wrote:
Hi everyone,
I understand the process of obtaining the internal integer codes for
the raw values of a factor (using as.numeric() as below), but what is
the best way to obtain these codes for the levels() of a factor (since
the as.numeric() results don't really make clear which code maps to
which level)?
fdata<-factor(c("b","b","c","a","b","c"),labels=c("I","II","III"))
fdata
levels(fdata)
as.numeric(fdata)
I thought something like this would make sense and work, but it throws an error:
as.numeric(levels(fdata))
seq_len(length(levels(fdata))) will give you the numeric codes for the levels. They are simply 1:3 in your example above. (Or perhaps I misunderstood your question?) Duncan Murdoch
Hello, Though I have the same doubt as Bert, the following seems to make more sense. seq_along(levels(fdata)) Hope this helps, Rui Barradas Em 25-03-2013 15:37, Dan Abner escreveu:
Hi everyone,
I understand the process of obtaining the internal integer codes for
the raw values of a factor (using as.numeric() as below), but what is
the best way to obtain these codes for the levels() of a factor (since
the as.numeric() results don't really make clear which code maps to
which level)?
fdata<-factor(c("b","b","c","a","b","c"),labels=c("I","II","III"))
fdata
levels(fdata)
as.numeric(fdata)
I thought something like this would make sense and work, but it throws an error:
as.numeric(levels(fdata))
Thanks!
Dan
______________________________________________ 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.
I would like to add that using the labels argument without a levels
argument in factor(),
fdata<-factor(c("b","b","c","a","b","c"),labels=c("I","II","III"))
is a dangerous way to make your factor.
Consider what would happen if the 4th input value were not "a" but
was "d" or if you were in a locale where the sort order is not "a", "b",
"c" (this is unlikely in this example, but if you had some capitalized and
some not the sort order would be different in the "C" locale and almost
any other locale).
Avoid the problem by stating what the input levels are expected to be:
fdata <- factor( c("b","b","c","a","b","c"), levels=c("a","b","c"), labels=c("I","II","III"))
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Rui Barradas Sent: Monday, March 25, 2013 9:38 AM To: Dan Abner Cc: r-help at r-project.org Subject: Re: [R] Obtaining the internal integer codes of a factor XXXX Hello, Though I have the same doubt as Bert, the following seems to make more sense. seq_along(levels(fdata)) Hope this helps, Rui Barradas Em 25-03-2013 15:37, Dan Abner escreveu:
Hi everyone,
I understand the process of obtaining the internal integer codes for
the raw values of a factor (using as.numeric() as below), but what is
the best way to obtain these codes for the levels() of a factor (since
the as.numeric() results don't really make clear which code maps to
which level)?
fdata<-factor(c("b","b","c","a","b","c"),labels=c("I","II","III"))
fdata
levels(fdata)
as.numeric(fdata)
I thought something like this would make sense and work, but it throws an error:
as.numeric(levels(fdata))
Thanks!
Dan
______________________________________________ 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.
______________________________________________ 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.