banner



How To Change Data Type In R

Convert Data Frame Column to Numeric in R (ii Examples) | Change Factor, Character & Integer

In this R tutorial, I'll explain how to catechumen a data frame column to numeric in R. No matter if you lot need to change the class of factors, characters, or integers, this tutorial will show you how to do it.

The article is structured as follows:

  • Creation of Example Information in R
  • Convert 1 Column to Numeric (Case 1)
  • Catechumen Multiple Columns to Numeric (Case two)
  • Further Resource for Handling Data Types

Permit's swoop right in!

Create Example Data

First nosotros need to create some data in R that we can utilise in the examples later on:

data                <-                data.                frame                (x1                =                c(                1,                5,                8,                ii                ),                # Create example data frame                x2                =                c(                3,                ii,                five,                ii                ),                    x3                =                c(                2,                7,                1,                2                )                )                data$x1                <-                equally                .                factor                (data$x1)                # Kickoff column is a gene                data$x2                <-                every bit                .                character                (data$x2)                # Second column is a graphic symbol                data$x3                <-                as                .                integer                (data$x3)                # 3rd column is an integer                information                # Print data to RStudio console              

data <- data.frame(x1 = c(1, 5, 8, ii), # Create instance information frame x2 = c(3, 2, five, 2), x3 = c(ii, vii, 1, 2)) data$x1 <- as.cistron(data$x1) # First column is a cistron data$x2 <- every bit.grapheme(information$x2) # Second column is a character information$x3 <- as.integer(data$x3) # Third column is an integer data # Print information to RStudio console

You can run into the construction of our instance information frame in Table 1. The data contains 3 columns: a cistron variable, a character variable, and an integer variable.

Example Data with Factor, Character and Integer Data Type

Tabular array ane: Example Data Frame with Gene, Character & Integer Variables.

We tin can check the class of each column of our information table with the sapply function:

sapply(data,                course                )                # Become classes of all columns                #       x1          x2          x3                                # "factor" "graphic symbol"   "integer"              

sapply(data, class) # Go classes of all columns # x1 x2 x3 # "cistron" "graphic symbol" "integer"

The data is ready, so let's move on to the examples…

Example one: Convert One Variable of Data Frame to Numeric

In the first example I'm going to convert only i variable to numeric. For this job, we tin can utilise the following R lawmaking:

data$x1                <-                as                .                numeric                (                every bit                .                graphic symbol                (information$x1)                )                # Catechumen 1 variable to numeric              

data$x1 <- as.numeric(as.grapheme(data$x1)) # Catechumen i variable to numeric

Note: The previous code converts our cistron variable to graphic symbol starting time and then it converts the character to numeric. This is important in guild to retain the values (i.e. the numbers) of the factor variable. Yous can learn more than about that in this tutorial.

However, permit'southward bank check the classes of our columns again to see how our information has changed:

sapply(data,                form                )                # Get classes of all columns                #        x1          x2          x3                                # "numeric" "grapheme"   "integer"              

sapply(data, class) # Get classes of all columns # x1 x2 x3 # "numeric" "character" "integer"

Every bit we wanted: The cistron column was converted to numeric.

If you need more than explanation on the R syntax of Example 1, you lot might have a look at the following YouTube video. In the video, I'1000 explaining the previous R programming code in some more than detail:

Case two: Change Multiple Columns to Numeric

In Instance 1 we used the as.numeric and the every bit.graphic symbol functions to alter i variable of our example data. All the same, when nosotros desire to change several variables to numeric simultaneously, the arroyo of Instance 1 might be too boring (i.e. too much programming). In this example, I'thou therefore going to show y'all how to alter as many columns equally you want at the same time.

First, we need to specify which columns nosotros desire to modify. In this example, we are converting columns 2 and 3 (i.e. the graphic symbol string and the integer):

i                <-                c(                2,                3                )                # Specify columns y'all desire to alter              

i <- c(2, 3) # Specify columns you want to change

We can now use the apply function to modify columns 2 and 3 to numeric:

information[                , i]                <-                apply(data[                , i],                2,                # Specify ain function within apply                function(x)                every bit                .                numeric                (                as                .                graphic symbol                (x)                )                )              

data[ , i] <- use(data[ , i], 2, # Specify ain part inside use function(x) as.numeric(as.character(x)))

Permit's cheque the classes of the variables of our data frame:

sapply(data,                form                )                # Get classes of all columns                #        x1        x2        x3                                # "numeric" "numeric" "numeric"              

sapply(data, class) # Get classes of all columns # x1 x2 x3 # "numeric" "numeric" "numeric"

The whole information frame was converted to numeric!

Further Resources

Converting variable classes in R is a complex topic. I take therefore listed some boosted resource nigh the Modification of R information classes in the post-obit.

If you desire to larn more than well-nigh the basic information types in R, I can recommend the post-obit video of the Data Camp YouTube channel:

Also, yous could have a look at the following R tutorials of this homepage:

  • How to Convert Factor to Numeric
  • How to Catechumen Character to Numeric
  • Convert Factor to Character
  • blazon.convert R Function
  • List of Useful R Functions
  • The R Programming Linguistic communication

I promise y'all liked this tutorial! Allow me know in the comments if you lot have any further questions and of crusade I am too happy about general feedback.

Source: https://statisticsglobe.com/convert-data-frame-column-to-numeric-in-r

Posted by: emerychadoicy.blogspot.com

0 Response to "How To Change Data Type In R"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel