Convert String to a unique integer in R

I have a data frame of this type

string1,string2,value1 string3,string1,value2 string3,string5,value3 ... ... 

I would convert srings to unique integers:

 1,2,value1 3,1,value2 3,5,value3 ... ... 

I am trying to use the c () operator, which converts a string to a unique integer. The problem is how to manage the two columns of the data frame. How can i do this?

+4
source share
2 answers

If you want to assign numbers to lines, rather than deleting the text "line", you can use a coefficient with known levels, and then force a number.

 d <- read.csv(header=TRUE, file=textConnection("a,b,c string1,string2,value1 string3,string1,value2 string3,string5,value3")) l=unique(c(as.character(d$a), as.character(d$b))) d1 <- data.frame(a=as.numeric(factor(d$a, levels=l)), b=as.numeric(factor(d$b, levels=l)), c=d$c) > d1 abc 1 1 3 value1 2 2 1 value2 3 2 4 value3 

Please note that the selected numerical values ​​do not match the numbers in the lines, but each line is assigned a unique number.

+8
source

Here's a simple solution using match :

 df <- read.csv(text="string1,string2,value1 string3,string1,value2 string3,string5,value3", header = FALSE) cbind(sapply(df[-3], match, unique(unlist(df[-3]))), df[3]) V1 V2 V3 1 1 3 value1 2 2 1 value2 3 2 4 value3 

How it works: the values ​​of both columns are mapped to the vector of unique numbers for these columns. This returns their position.

+3
source

All Articles