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
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().
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.
To convert String to Integer in R programming, call strtoi() function, pass the string and base values to this function.
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.
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)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With