'Extract rows in R dataframe which aligns with dummyvariable

I would like to create one vector of every MKT observation for when the dummy variable equals 1 and another vector when dummy equals 0.

This is how my dataframe looks:

enter image description here

r


Solution 1:[1]

Easy solution with dplyr:

df_new <- filter(SP500.df, dummy == 1)

Solution 2:[2]

You can do this with base R as well.

## Case when dummy = 1
MKT_vector = SP500.df[SP500.df$dummy==1,]$MKT

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 geoff
Solution 2 Deepansh Arora