Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R transform a factor ID variable into a numeric ID variable

Tags:

r

I have a programming doubt in R and I have no idea how to solve it after spending hours looking at potential responses on the internet and on Stack Overflow.

I have a factor variable in a column of a data.frame that looks like this:

Columnname
agsgssg
agsgssg
agsgssg
adgatata
ahagha
ahagha
ahagha
ahagha
aghaatah
ghssghs
ghssghs
ghssghs

The factor variable is not directly transformable into numeric with as.numeric(as.character()) because each level is a string, not a number.

What I would need is

Columnname            Numericcolumnname
agsgssg                        1
agsgssg                        1
agsgssg                        1
adgatata                       2
ahagha                         3   
ahagha                         3  
ahagha                         3   
ahagha                         3  
aghaatah                       4  
ghssghs                        5
ghssghs                        5   
ghssghs                        5  

I have tried several approaches including using levels() for the factor variable, using freq() for the factor variable trying to figure out how many rows there are for each level and then making a repeated number for each level of the factor with several "for" loops without success.

I feel that it should have a very simple solution, I am just not figuring it out.

Thank you for your consideration

like image 228
Ivan Avatar asked Feb 22 '16 16:02

Ivan


People also ask

How do I convert a factor to a numeric in R?

There are two steps for converting factor to numeric: Step 1: Convert the data vector into a factor. The factor() command is used to create and modify factors in R. Step 2: The factor is converted into a numeric vector using as. numeric().

How do I convert a factor to a numeric in a Dataframe in R?

We must first convert the factor vector to a character vector, then to a numeric vector. This ensures that the numeric vector contains the actual numeric values instead of the factor levels.

How do I change data from string to numeric in R?

To convert String to Integer in R programming, call strtoi() function, pass the string and base values to this function.

How do you factor variables in R?

To create a factor variable we use the factor function. The only required argument is a vector of values which can be either string or numeric.


1 Answers

In case, the levels are in different order, we can convert the column to factor with levels specified as the unique elements in that column, and then coerce it to numeric/integer.

df1$Numericcolumnname <- as.numeric(factor(df1$Columnname, 
                  levels=unique(df1$Columnname)))
like image 140
akrun Avatar answered Sep 27 '22 18:09

akrun