banner



How To Convert Data To Numeric In R

Factors are data structures which are implemented to categorize the data or represent categorical data and store information technology on multiple levels. They can be stored equally integers with a respective label to every unique integer. Though factors may look similar to character vectors, they are integers, and care must be taken while using them as strings. The factor accepts only a restricted number of distinct values. It is helpful in categorizing information and storing it on multiple levels.

Converting Factors to Numeric Values

At times you crave to explicitly change factors to either numbers or text. To achieve this, one has to use the functions as.character() or as.numeric(). There are ii steps for converting cistron to numeric: Pace one: Convert the data vector into a factor. The gene() command is used to create and modify factors in R. Step 2: The cistron is converted into a numeric vector using as.numeric(). When a factor is converted into a numeric vector, the numeric codes corresponding to the factor levels will be returned. Case: Take a data vector 'V' consisting of directions and its gene will be converted into numeric.

Python3

V = c("North", "Due south", "East", "East")

drn < - gene(V)

as.numeric(drn)

Output:

[i] 2 3 1 1

Converting a Factor that is a Number: If the cistron is number, outset convert it to a grapheme vector then to numeric. If a factor is a character and so you need non convert it to a character. And if you try converting an alphabet character to numeric it will return NA. Example: Suppose nosotros are taking costs of soaps of the diverse brands which are numbers with value south(29, 28, 210, 28, 29).

Python3

soap_cost < - factor(c( 29 , 28 , 210 , 28 , 29 ))

as.numeric(as.character(soap_cost))

Output:

[one]  29  28 210  28  29

However, if you simply utilise equally. numeric(), the output is a vector of the internal level representations of the factor and non the original values.

Python3

soap_cost < - factor(c( 29 , 28 , 210 , 28 , 29 ))

as.numeric(soap_cost)

Output:

[1] 2 i three 1 2

Converting Numeric value to a Factor

For converting a numeric into cistron nosotros use cut() office. cut() divides the range of numeric vector(assume 10) which is to be converted by cutting into intervals and codes its value (x) according to which interval they autumn. Level one corresponds to the leftmost, level 2 corresponds to the next leftmost, and and then on.

Syntax: cutting.default(x, breaks, labels = NULL, include.lowest = FALSE, correct = TRUE, dig.lab = iii)

where,

  • When a number is given through 'pause=' argument, the output factor is created past the sectionalization of the range of variables into that number of equal-length intervals.
  • In syntax include.lowest indicates whether an '10[i]' which equals the everyman (for correct= TRUE) suspension's value should be included. And 'right' in the syntax indicates whether the intervals should exist open up on the left and closed on the right or vice versa.
  • If labels are not provided and so dig.lab is used. The number of digits used in formatting the suspension numbers is determined through information technology.

Example 1: Lets u.s.a. assume an employee data set of historic period, salary and gender. To create a factor corresponding to age with iii equally spaced levels we can write in R as follows:

Python3

historic period < - c( 40 , 49 , 48 , 40 , 67 , 52 , 53 )

bacon < - c( 103200 , 106200 , 150200 , 10606 , 10390 , 14070 , 10220 )

gender < - c("male", "male", "transgender",

"female", "male", "female", "transgender")

employee< - data.frame(historic period, bacon, gender)

wfact = cutting(employee$age, 3 )

tabular array(wfact)

Output:

wfact (xl,49] (49,58] (58,67]        iv       2       1        

Example ii: Nosotros will now put labels- young, medium and anile.

Python3

age < - c( twoscore , 49 , 48 , 40 , 67 , 52 , 53 )

salary < - c( 103200 , 106200 , 150200 , 10606 , 10390 , 14070 , 10220 )

gender < - c("male", "male", "transgender",

"female", "male", "female", "transgender")

employee< - data.frame(age, salary, gender)

wfact = cut(employee$age, three , labels = c( 'Young' , 'Medium' , 'Aged' ))

table(wfact)

Output:

wfact  Young Medium   Anile       4      2      1        

The next examples volition use 'norm()' for generating multivariate normal distributed random variants within the specified space. There are three arguments given to rnorm():

  • due north: Number of random variables demand to be generated
  • mean: Its value is 0 by default if not mentioned
  • sd: standard deviation value needs to be mentioned otherwise information technology is 1 by default

Syntax:

norm(north, mean, sd)

Python3

y < - rnorm( 100 )

table(cutting(y, breaks = pi / 3 * ( - 3 : 3 )))

Output:

(-three.14,-2.09] (-ii.09,-ane.05]     (-1.05,0]      (0,1.05]   (one.05,2.09]              1            11            26            48            10    (2.09,3.14]              4        

The output factor is created by the division of the range of variables into v equal-length intervals through break argument.

Python3

age < - c( forty , 49 , 48 , twoscore , 67 , 52 , 53 )

gender < - c("male", "male", "transgender", "female person", "male", "female person", "transgender")

employee< - information.frame(age, gender)

wfact = cut(employee$age, breaks = 5 )

table(wfact)

Output:

wfact   (40,45.iv] (45.four,fifty.eight] (50.8,56.2] (56.2,61.6]   (61.6,67]            2           two           2           0           i        

Python3

y < - rnorm( 100 )

table(cutting(y, breaks = pi / iii * ( - 3 : three ), dig.lab = 5 ))

Output:

(-3.1416,-ii.0944] (-2.0944,-1.0472]       (-one.0472,0]        (0,1.0472]                  5                xiii                33                28    (i.0472,ii.0944]   (ii.0944,3.1416]                 19                 ii        

How To Convert Data To Numeric In R,

Source: https://www.geeksforgeeks.org/convert-factor-to-numeric-and-numeric-to-factor-in-r-programming/

Posted by: cokleyunfortuabood.blogspot.com

0 Response to "How To Convert Data To Numeric In R"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel