stacker
stacker

Reputation: 85

How do I filter my dataset on the combination of values in two columns in R

I want to be able to filter my data based on the combination of 2 values

Key   C1  D2

 D    25  25
 E    10  10
 F    50  0
 G    40  10

How do I make it so that I have the following output because I want only see data where c1 + c2 is >= 50

Key   C1  D2

 D    25  25
 F    50  0
 G    40  10

Upvotes: 0

Views: 64

Answers (3)

moodymudskipper
moodymudskipper

Reputation: 47300

df <- read.table(text="Key   C1  D2
 D    25  25
 E    10  10
 F    50  0
 G    40  10",h=T,strin=F)

df[df$C1 + df$D2 >=50,] # or subset(df, C1 + D2 >=50)
#   Key C1 D2
# 1   D 25 25
# 3   F 50  0
# 4   G 40 10

Upvotes: 1

patL
patL

Reputation: 2299

The dplyr approach:

library(dplyr)

df %>% 
   filter(C1 + D2 >= 50)

#  Key C1 D2
#1   D 25 25
#2   F 50  0
#3   G 40 10

Upvotes: 0

JineshEP
JineshEP

Reputation: 748

df1 <- read.table(text = "Key   C1  D2

                  D    25  25
                  E    10  10
                  F    50  0
                  G    40  10", header = TRUE)



df1[df1$C1 + df1$D2 >=50, ]

This gives the below output. Remember in R, all are vectorised operations, so the conditions gives a vector of true values as you require.

  Key C1 D2
1   D 25 25
3   F 50  0
4   G 40 10

Upvotes: 0

Related Questions