[R] numeric to factor via lookup table

David Winsemius dwinsemius at comcast.net
Sun Mar 30 00:38:44 CET 2014


On Mar 28, 2014, at 1:52 PM, Noah Marconi wrote:

> Is this what you're going for?
> 
> factor(values, levels=mylevels[[1]], labels=mylevels[[2]])
> 
> [1] a b e e j
> Levels: a b c d e f g h i j

That is certainly what looks like the right (or at least obvious) answer to me. The OP should note that although the argument to the 'levels' parameter was used on input, that now when you apply the `levels` function to that object (had you assigned it a name) you will be returned the value given to 'labels'.

-- 
David.
> 
> 
> On 2014-03-28 16:38, Jonathan Greenberg wrote:
>> R-helpers:
>> Hopefully this is an easy one.  Given a lookup table:
>> mylevels <- data.frame(ID=1:10,code=letters[1:10])
>> And a set of values (note these do not completely cover the mylevels range):
>> values <- c(1,2,5,5,10)
>> How do I convert values to a factor object, using the mylevels to
>> define the correct levels (ID matches the values), and code is the
>> label?
>> --j
>> --
>> Jonathan A. Greenberg, PhD
>> Assistant Professor
>> Global Environmental Analysis and Remote Sensing (GEARS) Laboratory
>> Department of Geography and Geographic Information Science
>> University of Illinois at Urbana-Champaign
>> 259 Computing Applications Building, MC-150
>> 605 East Springfield Avenue
>> Champaign, IL  61820-6371
>> Phone: 217-300-1924
>> http://www.geog.illinois.edu/~jgrn/
>> AIM: jgrn307, MSN: jgrn307 at hotmail.com, Gchat: jgrn307, Skype: jgrn3007
>> ______________________________________________
>> 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.

David Winsemius
Alameda, CA, USA




More information about the R-help mailing list