Hi,
Try:
df2 <- df 
df2[] <- lapply(df2,function(x) 
as.character(as.numeric(factor(x,levels=unique(df$G1))))) 

A.K.


On Sunday, April 13, 2014 3:36 PM, Sergio.pv <serpalm...@gmail.com> wrote:
I have a data.frame of two vectors.

df <- data.frame(G1=c("b","a","e","d","c"),
                 G2=c("c","d","e","b","a"))
You can see that both vectors have the same characters, but in diferent
order. I want to convert them into numbers and then compare them.

To compare G2 to G1, G1 must be the reference, so the output will be this:

df2 <- data.frame(G1=c("1","2","3","4","5"),
                 G2=c("5","4","3","1","2"))
Is there a way to do this?, thanks



--
View this message in context: 
http://r.789695.n4.nabble.com/Transform-characters-to-numbers-and-compare-tp4688708.html
Sent from the datatable-help mailing list archive at Nabble.com.
_______________________________________________
datatable-help mailing list
datatable-h...@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help

______________________________________________
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.

Reply via email to