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,130 points
64 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,560 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
92 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 72 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
• 32,440 points
45 views
0 votes
0 answers

How to get the value passed through url from one page to another in R?

How to get the value passed through ...READ MORE

Oct 16, 2019 in Data Analytics by ch
• 3,380 points
25 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
216 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
• 51,830 points
98 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,560 points
3,554 views
0 votes
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
• 32,440 points
1,301 views