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
Bert Gunter
2013-Mar-25 15:49 UTC
[R] Obtaining the internal integer codes of a factor XXXX
Why do you think you need to do this? (Feel free to ignore, however). -- Bert On Mon, Mar 25, 2013 at 8:37 AM, Dan Abner <dan.abner99@gmail.com> 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)) > > Thanks! > > Dan > > ______________________________________________ > R-help@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. >-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm [[alternative HTML version deleted]]
Duncan Murdoch
2013-Mar-25 16:18 UTC
[R] Obtaining the internal integer codes of a factor XXXX
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
Rui Barradas
2013-Mar-25 16:37 UTC
[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. >