'Create dataframe if one column = a certain value and another column = another certain value

I'm trying to basically take a dataframe that has multiple columns, 2 of which are State and Sex, and create a new dataframe from that data only if State = 'United States' and Sex = 'All Sexes" but I can't figure out how.

Right now I'm trying to do it this way, where I'm pulling out a dataframe for the united states data and then pulling out another dataframe from the states data for the sex data.

# create data frame with just united states total data
us_data <- df[df$State == 'United States',] 

# create data frame with just female and male sex data
all_data <- df[us_data$Sex == 'All Sexes',]


Solution 1:[1]

Figured it out!

all_data <- subset(df, (State == 'United States' & Sex != 'All Sexes'))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tori