quit

R demo: Using the 'as.numeric' and 'as.factor' functions with data frames



Sharing buttons:

okay in this video I'm going to show you

how to utilize the as numeric and as

factored functions in order to change

the characteristics of variables within

a data frame so what I have right here

is I'm actually just going to

demonstrate new data right here this is

a data frame that that I've been

utilizing a couple of videos we have

variables of political party interest

motivate achieve and gender and what I

want to do is to let's say let's take a

quick look at the structure of the

variables that aren't contained in this

data frame so the data frame is called

new data we're going to use the STR

function in order to look at it so you

can see that Paul party is recognized as

a factor interest is recognized as

numeric as are the remaining variables

so let's say that I want to change the

interest variable I want to convert this

into a factor well we can do this pretty

easily in the following way first off we

type in the name of the data frame which

is new data in this case follow that up

with a dollar sign and then the variable

that we want to convert so in this case

again we're converting interest from a

numeric variable to one that is a factor

variable so I'm going to type in

interest which is the name of the

variable that we're that we're

converting so now we will type in an

arrow and then use the as period numeric

scuse me factor function will type in

new parentheses here and we'll type in

new data dollar sign and interest and

then in parentheses so notice what's

going on where this is basically

indicating the data frame we're working

with and the variable that we are that

we're that we're changing the

characteristics on we can just we

basically say that we are coercing this

variable from numeric to factor using

the ask dot factor function and so so

this is just

the function is applied to this so that

the new data frame will contain the new

version of interest so I'm going to hit

enter and then type in new data and you

can see it looks the same but now when

we type in STR for structure and then

new data you'll see that now the

interest variable is recognized as being

a factor

so we've now converted the

characteristics of the interest variable

from numeric to factor let's say that we

want to convert it back to numeric then

we can use the as numeric function so

what we'll do is we'll just again we'll

type a new data dollar sign and interest

and then our arrow and then we'll type

an ass period numeric and then we'll

type in new data dollar sign interest

and then enter and so now when we look

at the structure of our of our data you

can see that now the interest variable

has been converted back to numeric so

that you know that's basically the

application of the ass factor as numeric

functions the reason why this can be

really important is because when you're

working and are with different functions

you know when you're trying to carry out

different analyses they those analyses

the functions that are utilized make

assumptions about the characteristics of

your data and you know some may make you

know may assume that a variable is

numeric but if your data is in as being

recognized as a factor then you're going

to need to convert that numeric variable

into a factor and vice versa so this is

just a quick way in order to be able to

do that if you didn't want to do it this

way one of the previous video that I

showed you you could accomplish the same

things by going through the Edit data

editor option right here and if we type

in the name of the data frame which is

new data in this case click on OK you

know we can go back and forth we can

change it to character which in our a

character variable is going to be

recognized as a factor to be treated as

such so if I change that and then click

out of here and then look at the

structure you can see that it's being

recognized as a character but in terms

of its usage and statistical operations

is going to be recognized and treated as

a factor if I want to convert this back

to to numeric then I could certainly do

that just kind of go back into the the

data editor and you know change this to

type numeric and so now when we look at

our variable you can see that now

interest is again being recognized as

numeric