0

I have a data frame with 3 columns

   a b c
P1 z 1 22
P2 z 2 18
P3 x 1 3
P4 z 1 16
P5 x 2 6
P6 z 1 12
P7 x 1 23

I want to create a new data frame with only the values in column "c" which meet the condition that there is a "z" in column "a" and a "1" in column "b" + "x" in column "a" and a "2" in column "b".

Afterwards the new data frame should look like this:

   a b c
P1 z 1 22
P4 z 1 16
P5 x 2 6
P6 z 1 12

The first thing that comes in mind is to use subset(). But I don't know how. Thank you for your help.

RenaSo
  • 159
  • 2
  • 2
  • 6

1 Answers1

1

Using subset the following reproduces your expected output:

subset(df, (a == "z" & b == 1) | (a == "x" & b == 2));
#   a b  c
#P1 z 1 22
#P4 z 1 16
#P5 x 2  6
#P6 z 1 12

Or with a dplyr approach using filter:

library(dplyr);
filter(df, (a == "z" & b == 1) | (a == "x" & b == 2))

Sample data

df <- read.table(text = 
    "   a b c
P1 z 1 22
P2 z 2 18
P3 x 1 3
P4 z 1 16
P5 x 2 6
P6 z 1 12
P7 x 1 23", header = T)
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68