Hi,
I know how to convert a factor-variable into a numeric variable via
as.numeric().
But how can I control the values that are assigned?
For example, I have this factor-variable:
z <- c("male", "male", "female")
z <- as.factor(z)
And I want to convert male in 3 and female into the numeric value 5
(just for the example)
so that I get:
[1] 3 3 5
Thanks for any help!
converting a factor in numeric values with direct control of the numeric values
7 messages · Jörg Groß, ronggui, David Winsemius +2 more
I think you have to recode the derived variable of as.numeric(z).
On Sun, Jan 18, 2009 at 12:40 AM, J?rg Gro? <joerg at licht-malerei.de> wrote:
Hi,
I know how to convert a factor-variable into a numeric variable via
as.numeric().
But how can I control the values that are assigned?
For example, I have this factor-variable:
z <- c("male", "male", "female")
z <- as.factor(z)
And I want to convert male in 3 and female into the numeric value 5 (just
for the example)
so that I get:
[1] 3 3 5
Thanks for any help!
______________________________________________ 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.
HUANG Ronggui, Wincent Tel: (00852) 3442 3832 PhD Candidate Dept of Public and Social Administration City University of Hong Kong Homepage: http://ronggui.huang.googlepages.com/
BTW, a better way to convert factor into a numeric variable is to use unclass().
On Sun, Jan 18, 2009 at 12:40 AM, J?rg Gro? <joerg at licht-malerei.de> wrote:
Hi,
I know how to convert a factor-variable into a numeric variable via
as.numeric().
But how can I control the values that are assigned?
For example, I have this factor-variable:
z <- c("male", "male", "female")
z <- as.factor(z)
And I want to convert male in 3 and female into the numeric value 5 (just
for the example)
so that I get:
[1] 3 3 5
Thanks for any help!
______________________________________________ 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.
HUANG Ronggui, Wincent Tel: (00852) 3442 3832 PhD Candidate Dept of Public and Social Administration City University of Hong Kong Homepage: http://ronggui.huang.googlepages.com/
On Sat, 17 Jan 2009, J?rg Gro? wrote:
Hi,
I know how to convert a factor-variable into a numeric variable via
as.numeric().
But how can I control the values that are assigned?
For example, I have this factor-variable:
z <- c("male", "male", "female")
z <- as.factor(z)
And I want to convert male in 3 and female into the numeric value 5 (just for
the example)
so that I get:
[1] 3 3 5
Like this:
z <- c("male", "male", "female")
z <- as.factor(z)
num.codes[ levels(z) ][ z ]
male male female
3 3 5
or if you want to avoid the names():
z.to.num <- unname(num.codes)[ match(levels(z),names(num.codes)) ] z.to.num[ z ]
[1] 3 3 5
See ?as.character ?match ?levels HTH, Chuck
Thanks for any help!
______________________________________________ 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.
Charles C. Berry (858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
ifelse is your friend:
> z <- c("male", "male", "female")
> z <- as.factor(z)
>
> z <- ifelse(z == "male", 3, 5)
> z
[1] 3 3 5
> class(z)
[1] "numeric"
>
--
David Winsemius
On Jan 17, 2009, at 11:40 AM, J?rg Gro? wrote:
Hi,
I know how to convert a factor-variable into a numeric variable via
as.numeric().
But how can I control the values that are assigned?
For example, I have this factor-variable:
z <- c("male", "male", "female")
z <- as.factor(z)
And I want to convert male in 3 and female into the numeric value 5
(just for the example)
so that I get:
[1] 3 3 5
Thanks for any help!
______________________________________________ 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.
On Sat, 17 Jan 2009, Charles C. Berry wrote:
On Sat, 17 Jan 2009, J?rg Gro? wrote:
Hi,
I know how to convert a factor-variable into a numeric variable via
as.numeric().
But how can I control the values that are assigned?
For example, I have this factor-variable:
z <- c("male", "male", "female")
z <- as.factor(z)
And I want to convert male in 3 and female into the numeric value 5 (just
for the example)
so that I get:
[1] 3 3 5
Like this:
z <- c("male", "male", "female")
z <- as.factor(z)
Oops! I forgot to copy this line
num.codes <- c(male = 3, female = 5 )
num.codes[ levels(z) ][ z ]
male male female
3 3 5
or if you want to avoid the names():
z.to.num <- unname(num.codes)[ match(levels(z),names(num.codes)) ] z.to.num[ z ]
[1] 3 3 5
See ?as.character ?match ?levels HTH, Chuck
Thanks for any help!
______________________________________________ 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.
Charles C. Berry (858) 534-2098
Dept of Family/Preventive
Medicine
E mailto:cberry at tajo.ucsd.edu UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
Charles C. Berry (858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
ronggui wrote:
I think you have to recode the derived variable of as.numeric(z).
The easiest way is probably to use indexing:
> z <- c("male", "male", "female")
> z <- factor(z)
> c(5,3)[z]
[1] 3 3 5
or, slightly more foolproof
> c(male=3,female=5)[as.character(z)]
male male female
3 3 5
On Sun, Jan 18, 2009 at 12:40 AM, J?rg Gro? <joerg at licht-malerei.de> wrote:
Hi,
I know how to convert a factor-variable into a numeric variable via
as.numeric().
But how can I control the values that are assigned?
For example, I have this factor-variable:
z <- c("male", "male", "female")
z <- as.factor(z)
And I want to convert male in 3 and female into the numeric value 5 (just
for the example)
so that I get:
[1] 3 3 5
Thanks for any help!
______________________________________________ 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.
O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907