

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert a factor that is represented by numeric values to integer or numeric variable in R?
<p>We can convert a factor to integer or numeric variable by using as.numeric function with defining the levels of the factor or by defining the characters of the factor</p><h2>Example</h2><pre class="prettyprint notranslate">> f <- factor(sample(runif(10), 10, replace = TRUE)) > f [1] 0.323049098020419 0.916131897130981 0.271536672720686 0.462429489241913 [5] 0.657008627429605 0.462429489241913 0.462429489241913 0.212830029195175 [9] 0.271536672720686 0.497305172728375 7 Levels: 0.212830029195175 0.271536672720686 ... 0.916131897130981</pre><p>Using as.numeric</p><pre class="result notranslate">> as.numeric(levels(f))[f] [1] 0.3230491 0.9161319 0.2715367 0.4624295 0.6570086 0.4624295 0.4624295 [8] 0.2128300 0.2715367 0.4973052 > Using as.numeric(as.character( )) > as.numeric(as.character(f)) [1] 0.3230491 0.9161319 0.2715367 0.4624295 0.6570086 0.4624295 0.4624295 [8] 0.2128300 0.2715367 0.4973052</pre>
- Related Questions & Answers
- How to convert numeric columns to factor using dplyr package in R?
- How to convert a data frame column to date that contains integer values in R?
- How to convert numeric levels of a factor into string in R data frame?
- Convert a numeric column to binary factor based on a condition in R data frame
- How to convert the data type of all columns from integer to factor in R?
- How to convert a data frame into table for two factor columns and one numeric column in R?
- How to sum a variable by factor levels in R?
- How to convert factor levels into character in R?
- How to convert data frame values to a vector by rows in R?
- How to convert a numerical column into factor column in R?
- How to convert more than one column in R data frame to from integer to numeric in a single line code?
- How to find the mean of very small numbers in numeric form that are represented with scientific notation in R?
- How to check if all values in a vector are integer or not in R?
- How to convert multiple numerical variables to factor variable in R?
- How to convert a character data frame to numeric data frame in R?
Advertisements