Changing the column type from numeric to factor - R

0 votes

I am working with the "mtcars" dataset, below are few rows from it:

                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

The 'cyl' and 'gear' columns from this dataset are of numeric type, i want to change their class to factor, how can i do it?

May 8, 2018 in Data Analytics by DataKing99
• 8,100 points
27 views

1 answer to this question.

0 votes

You can use the as.factor() function for this purpose:

as.factor(mtcars$cyl)->mtcars$cyl

I am converting the type of 'cyl' from numeric to factor and storing it back to 'cyl' column.

Similarly,

as.factor(mtcars1$gear)->mtcars1$gear

answered May 8, 2018 by Bharani
• 4,550 points

Related Questions In Data Analytics

0 votes
1 answer

How can I convert a factor variable to numeric in R ?

A factor variable can be converted to ...READ MORE

answered Sep 18, 2018 in Data Analytics by shams
• 3,580 points
28 views
0 votes
1 answer

Is it possible to change the column names of the dataset that I'm using in R?

That is definitely possible in R. You ...READ MORE

answered Dec 17, 2018 in Data Analytics by Anisha

edited Dec 17, 2018 30 views
0 votes
1 answer

How to Share the files from local system Mac Book to Edureka VM

Hey Satya, Hope you're doing great. Would you please ...READ MORE

answered Dec 18, 2017 in Data Analytics by Sudhir
• 1,610 points
37 views
0 votes
1 answer

With the help of tidyverse: how to rename a column to a variable name

With the help of Dplyr: rename function ...READ MORE

answered Apr 3, 2018 in Data Analytics by DeepCoder786
• 1,700 points

edited Apr 3, 2018 by DeepCoder786 29 views
0 votes
1 answer

How to convert a list of vectors with various length into a Data.Frame?

We can easily use this command as.data.frame(lapply(d1, "length< ...READ MORE

answered Apr 4, 2018 in Data Analytics by DeepCoder786
• 1,700 points
45 views
0 votes
2 answers

In data frame how to spilt strings into values?

You can do this using dplyr and ...READ MORE

answered Dec 4, 2018 in Data Analytics by Kalgi
• 37,320 points
36 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Extracting numeric columns from a data.frame - R

You can use the select_if() function from ...READ MORE

answered May 4, 2018 in Data Analytics by Bharani
• 4,550 points
667 views
0 votes
1 answer

Locating row index of a column which has the maximum value - R

which.max(iris$Sepal.Length) This command will give you the desired ...READ MORE

answered May 16, 2018 in Data Analytics by Bharani
• 4,550 points
59 views