LDT
LDT

Reputation: 3088

filter rows based on their Sum in dplyR R

I am trying to get things right using the dplyr package in R. Imagine that I have the iris dataset, which looks like this

library(tidyverse)
iris=iris[,1:4]
head(iris)

  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

I want to keep only the rows whose sum is bigger or equal (>=) 10. With baseR i can do it like this

iris[rowSums(iris) >= 10, , drop = FALSE]

How could do I do this using dplyR and the rowSums function

Upvotes: 0

Views: 944

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388817

You can use -

library(dplyr)

iris1=iris[,1:4]
iris1 %>% filter(rowSums(.) >= 10)

Upvotes: 0

Related Questions