Re: [R] numeric to factor via lookup table
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: jgrn...@hotmail.com, Gchat: jgrn307, Skype: jgrn3007 >> __ >> 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. > > __ > 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. David Winsemius Alameda, CA, USA __ 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.
Re: [R] numeric to factor via lookup table
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 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: jgrn...@hotmail.com, Gchat: jgrn307, Skype: jgrn3007 __ 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. __ 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.
Re: [R] numeric to factor via lookup table
On Mar 28, 2014, at 3:38 PM, 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 One approach would be to use ?merge and specify the 'by.*' arguments using column indices, where 'values' is column 1 and you want to match that with mylevels$ID, which is also column 1. Hence: > merge(values, mylevels, by.x = 1, by.y = 1) x code 1 1a 2 2b 3 5e 4 5e 5 10j Regards, Marc Schwartz __ 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.
Re: [R] numeric to factor via lookup table
Note that your example may be misleadingly simple, so I made it a bit more complicated. The key is ?match > mylevels <- data.frame(ID=10:1,code=letters[1:10]) > values <- c(1,2,5,5,10) > with(mylevels,code[match(values,ID)]) [1] j i f f a Levels: a b c d e f g h i j ## Note that you may have to take care if there are NA's in values. Read ?match carefully if this might happen. Cheers, Bert > Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." H. Gilbert Welch On Fri, Mar 28, 2014 at 1:38 PM, 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: jgrn...@hotmail.com, Gchat: jgrn307, Skype: jgrn3007 > > __ > 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. __ 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.
[R] numeric to factor via lookup table
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: jgrn...@hotmail.com, Gchat: jgrn307, Skype: jgrn3007 __ 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.