How to convert DataFrame column from Character to Numeric in R ?
Last Updated :
06 Feb, 2023
In this article, we will discuss how to convert DataFrame column from Character to Numeric in R Programming Language.
All dataframe column is associated with a class which is an indicator of the data type to which the elements of that column belong to. Therefore, in order to simulate the data type conversion, the data elements have to be converted to the desired data type in this case, that is all the elements of that column should be eligible to become numerical values.
Note: sapply() method can be used to retrieve the data type of the column variables in the form of a vector.
Method 1 : Using transform() method
The character type columns, be single characters or strings can be converted into numeric values only if these conversions are possible. Otherwise, the data is lost and coerced into missing or NA values by the compiler upon execution.
This approach depicts the data loss due to the insertion of missing or NA values in place of characters. These NA values are introduced since interconversion is not directly possible.
R
# declare a dataframe
# different data type have been
# indicated for different cols
data_frame <- data.frame(
col1 = as.character(6 : 9),
col2 = factor(4 : 7),
col3 = letters[2 : 5],
col4 = 97 : 100, stringsAsFactors = FALSE)
print("Original DataFrame")
print (data_frame)
# indicating the data type of
# each variable
sapply(data_frame, class)
# converting character type
# column to numeric
data_frame_col1 <- transform(data_frame,
col1 = as.numeric(col1))
print("Modified col1 DataFrame")
print (data_frame_col1)
# indicating the data type of
# each variable
sapply(data_frame_col1, class)
# converting character type column
# to numeric
data_frame_col3 <- transform(data_frame,
col3 = as.numeric(col3))
print("Modified col3 DataFrame")
print (data_frame_col3)
# indicating the data type of each
# variable
sapply(data_frame_col3, class)
Output:

Explanation: Using the sapply() method, the class of the col3 of the dataframe is a character, that is it consists of single-byte character values, but on the application of transform() method, these character values are converted to missing or NA values, because the character is not directly convertible to numeric data. So, this leads to data loss.
The conversion can be made by not using stringAsFactors=FALSE and then first implicitly converting the character to factor using as.factor() and then to numeric data type using as.numeric(). The information about the actual strings is completely lost even in this case. However, the data becomes ambiguous and may lead to actual data loss. The data is simply assigned numeric values based on the lexicographic sorting result of the column values.
R
# declare a dataframe
# different data type have been
# indicated for different cols
data_frame <- data.frame(
col1 = as.character(6 : 9),
col2 = factor(4 : 7),
col3 = c("Geeks", "For", "Geeks", "Gooks"),
col4 = 97 : 100)
print("Original DataFrame")
print (data_frame)
# indicating the data type of
# each variable
sapply(data_frame, class)
# converting character type column
# to numeric
data_frame_col3 <- transform(data_frame,
col3 = as.numeric(as.factor(col3)))
print("Modified col3 DataFrame")
print (data_frame_col3)
# indicating the data type of each
# variable
sapply(data_frame_col3, class)
Output:
[1] "Original DataFrame"
col1 col2 col3 col4
1 6 4 Geeks 97
2 7 5 For 98
3 8 6 Geeks 99
4 9 7 Gooks 100
col1 col2 col3 col4
"factor" "factor" "factor" "integer"
[1] "Modified col3 DataFrame"
col1 col2 col3 col4
1 6 4 2 97
2 7 5 1 98
3 8 6 2 99
4 9 7 3 100
col1 col2 col3 col4
"factor" "factor" "numeric" "integer"
Explanation: The first and third-string in col3 are the same therefore, assigned the same numeric value. And in total, the values are sorted in ascending order and then assigned corresponding integer values. "For" is the smallest string appearing in lexicographic order, therefore, assigned numeric value of 1, then "Geeks", both instances of which are mapped to 2 and "Gooks" is assigned a numeric value of 3. Thus, the col3 type changes to numeric.
Method 2 : Using apply() method
The apply() method in R allows the application of a function over multiple columns together. The function may be user-defined or inbuilt, depending upon user's need.
Syntax: apply ( df , axis , FUN)
Arguments :
- df - The dataframe to apply the function on
- axis - The axis to apply the function upon
- FUN- User-defined method to apply
Example:
R
# declare a dataframe
# different data type have been
# indicated for different cols
data_frame <- data.frame(
col1 = as.character(6:9),
col2 = as.character(4:7),
col3 = c("Geeks","For","Geeks","Gooks"),
col4 = letters[1:4])
print("Original DataFrame")
print (data_frame)
# indicating the data type of each
# variable
sapply(data_frame, class)
# defining the vector of columns to
# convert to numeric
vec <- c(1,2)
# apply the conversion on columns
data_frame[ , vec] <- apply(data_frame[ , vec,drop=F], 2,
function(x) as.numeric(as.character(x)))
print("Modified DataFrame")
print (data_frame)
# indicating the data type of each variable
sapply(data_frame, class)
Output:
[1] "Original DataFrame"
col1 col2 col3 col4
1 6 4 Geeks a
2 7 5 For b
3 8 6 Geeks c
4 9 7 Gooks d
col1 col2 col3 col4
"factor" "factor" "factor" "factor"
[1] "Modified DataFrame"
col1 col2 col3 col4
1 6 4 Geeks a
2 7 5 For b
3 8 6 Geeks c
4 9 7 Gooks d
col1 col2 col3 col4
"numeric" "numeric" "factor" "factor"
Explanation: The col1 and col2 types are converted to numeric. However, this method is applicable to pure numeric data converted to character. It throws an error "NAs introduced by coercion" upon execution for col3 and col4.
Similar Reads
How to convert dataframe columns from factors to characters in R?
In this article, we will discuss how to convert dataframe columns from factors to characters in R Programming Language. A dataframe can have different types of columns stacked together to form a tubular structure. Easy modification of the columns' data as well as conversion between data types can be
5 min read
How to Convert Character to Numeric in R?
In this article, we will discuss how to convert characters to numeric in R Programming Language. We can convert to numeric by using as.numeric() function. Syntax: as.numeric(character) where, character is an character vector Example: R # create a vector with 5 characters data = c('1', '2', '3', '4',
1 min read
Convert DataFrame Column to Numeric in R
In this article, we are going to see how to convert DataFrame Column to Numeric in R Programming Language. All dataframe column is associated with a class which is an indicator of the data type to which the elements of that column belong to. Therefore, in order to simulate the data type conversion,
9 min read
How to Convert Character to Factor in R?
The as.factor() method in R Programming Language is used to convert the character vector to factor class. Converting Character Vector To Factor Syntax: as.factor(char-vec) where char-vec is the character vector The class indicative of the data type of the vector can be obtained using the class() me
2 min read
How to Convert Factor to Character in R?
In this article, we will discuss how to convert the Factor Class to the character Class in the R Programming Language. Method 1: Convert a Single Factor Vector to Character Vector To convert a single factor vector to a character vector we use the as.character() function of the R Language and pass th
4 min read
How to add a prefix to column names in R DataFrame ?
In this article, we will discuss how to add prefixes to column names in DataFrame in R Programming Language. Dataset in use: First SecondThird1a72ab83cv94dsd10Method 1 : Using paste() method In order to modify the column names, the paste function in R can be used. The paste() method, can be used for
4 min read
Convert dataframe rows and columns to vector in R
In this article, we are going to convert a dataframe column to a vector and a dataframe row to a vector in the R Programming Language. Convert dataframe columns into vectors We are taking a column in the data frame and passing it into another variable by using the selection method. The selection met
2 min read
Convert two columns of a data frame to a named vector in R
In this article, we will discuss how to convert two columns of a dataframe to a named vector in R Programming Language. Let see the implementation stepwise: Example 1: Creating dataframe and convert columns to vector. Step 1: Here we create a DataFrame with name data. There is a total of two columns
2 min read
Convert dataframe column to list in R
In this article, we will learn how to convert a dataframe into a list by columns in R Programming language. We will be using as.list() function, this function is used to convert an object to a list. These objects can be Vectors, Matrices, Factors, and data frames. Syntax: as.list( object ) Parameter
2 min read
Convert Character Matrix to Numeric Matrix in R
In this article, we are going to see how to convert a given character matrix to numeric in R Programming Language. Converting the Character Matrix to Numeric Matrix we will use as.numeric() & matrix() Functions. Functions Usedas.numeric() function: This function is used to convert a given column
3 min read