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,250 points
1,129 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,660 points

Related Questions In Data Analytics

0 votes
1 answer
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 19, 2018 in Data Analytics by shams
• 3,670 points
3,823 views
+1 vote
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 1,745 views
0 votes
1 answer

How to edit the data source from R?

Use edit function like below and pass ...READ MORE

answered Oct 14, 2019 in Data Analytics by Cherukuri
• 33,030 points
1,073 views
+1 vote
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,720 points
1,700 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 5, 2018 in Data Analytics by Kalgi
• 52,350 points
1,131 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,660 points
7,757 views
+1 vote
2 answers

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

Hi, Nirvana You can also try this. which(iris$Sepal.Length == ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
13,045 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP