Extracting numeric columns from a data frame - R

0 votes

I am working with the iris data-set and want to extract only the numeric columns from it:

      Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2     setosa
2          4.9         3.0          1.4         0.2     setosa
3          4.7         3.2          1.3         0.2     setosa
4          4.6         3.1          1.5         0.2     setosa
5          5.0         3.6          1.4         0.2     setosa
6          5.4         3.9          1.7         0.4     setosa

For this data-set and in general if i wanted to extract only the numeric columns, how can i do it?

May 4, 2018 in Data Analytics by nirvana
• 3,130 points
7,740 views

1 answer to this question.

0 votes

You can use the select_if() function from the dplyr package for this purpose:

iris %>% select_if(is.numeric)->iris_1

The above code will solve your problem:

head(iris_1)
       Sepal.Length Sepal.Width Petal.Length Petal.Width
1          5.1         3.5          1.4         0.2
2          4.9         3.0          1.4         0.2
3          4.7         3.2          1.3         0.2
4          4.6         3.1          1.5         0.2
5          5.0         3.6          1.4         0.2
6          5.4         3.9          1.7         0.4
answered May 4, 2018 by Bharani
• 4,660 points
select_if(iris,is.numeric)

Exactly worked!. Thanks

Related Questions In Data Analytics

0 votes
1 answer

Extracting specific columns from a data frame

Yes, there is a more efficient method ...READ MORE

answered Jun 22, 2023 in Data Analytics by anonymous
• 1,380 points
838 views
+1 vote
2 answers

How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
1,812 views
0 votes
1 answer

Drop unused levels from a data frame in R

You can use this command droplevels() y <- ...READ MORE

answered Jun 14, 2018 in Data Analytics by DataKing99
• 8,250 points
1,979 views
0 votes
1 answer

Create a tree model in R from data.frame?

See the below example to understand how ...READ MORE

answered Aug 30, 2019 in Data Analytics by anonymous
• 33,030 points
1,742 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,661 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,099 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Discarding duplicate rows from a data.frame - R

You can use distinct() function along with ...READ MORE

answered May 4, 2018 in Data Analytics by Bharani
• 4,660 points
744 views
+1 vote
2 answers

Finding number of missing values and removing those missing values from a data-frame

To find number of missing values for ...READ MORE

answered Aug 14, 2019 in Data Analytics by anonymous
1,099 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