Re: [R] from character to numeric over multiple columns

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Fri 21 Jul 2006 - 20:51:15 EST

Are the columns factors or character? I'll try to write code that copes with both:

nm <- unique(c(as.character(col1), as.character(col2), as.character(col3)))

DF[] <- lapply(DF, function(x) match(x, nm))

On Fri, 21 Jul 2006, Federico Calboli wrote:

> Hi All,
>
> I have a data frame of three columns, all of which names. The names in the three
> cols overlap up to a point, so I might have *Harry* in all three columns,
> *Tom* in cols 1 and 3 and *Bob* in col 3 only.
>
> harry paul bob
> anita harry tom
> frank jack harry
> tom pete ben
> ....
>
> I want to turn the names into numbers, BUT I want the numeric code for, say,
> *Harry*, to be the same on all columns.
>
> Doing
>
> cbind(as.numeric(col1), as.numeric(col2), as.numeric(col3))
>
> does not work because the factors are different in each column, hence they get a
> different number even though the name is the same.
>
> Ideas?
>
> Cheers
>
> Federico
>
>

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-help@stat.math.ethz.ch 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.
Received on Fri Jul 21 20:54:48 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 21 Jul 2006 - 22:17:53 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.