Create new dataframe based on values from multiple columns

Hello everyone, I'm really hoping someone can help me since I'm a third year student and not too confident when using RStudio!

So, say I have this dataframe:
x = c(1, 1, 1, 1, 1, 0, 0, 0, 0, 0)
y = c(1, 0, 0, 1, 0, 1, 1, 0, 1, 0)
z = c(1, 0, 1, 0, 1, 0, 1, 0, 1, 0)
df <- data.frame(x, y, z)

I want to create two new dataframes based on the values of x and y.
If x=1 OR y=1 --> copy whole row into a dataframe (lets name it 'positive')
If x=0 AND y=0 --> copy whole row into a dataframe (lets name it 'zero')

I tried using split and then but this does not give a correct outcome.
Thanks in advance!

the logical OR operator in R is a vertical line | , and the logical AND is an ampersand &.
Testing for equality is by the double equals symbol ==.
Copying is equivalent to assigning an object to a new name
new <- old
finaly, filter() is a powerful function to subset a dataframe or tibble. it is part of the tidyverse package, that you can install once, and load into your environment as needed by library(tidyverse)

positive <- filter(df,
                  x==1 | y==1)

zero <- filter(df,
                x==0 & y==0)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.