Blayne Carpendale
Blayne Carpendale

Reputation: 1

Creating dummy variables in R for specific date ranges

I'm trying to create dummy variables in R for monthly premiums in certain data ranges, as in, a dummy that's 1 for 2009 to 2011 and a dummy that's 1 for 2017 to 2019. This is the data I'm working with, and I'm very much still new to R, so any comprehensive answer would be appreciated.

pic of data

Upvotes: 0

Views: 1289

Answers (1)

jomcgi
jomcgi

Reputation: 106

A combination of mutate and case_when from dplyr will let you define your dummy variables using logical statements.

mutate creates a new column or overwrites an existing one.

case_when checks each logical statement in order and applying value you provide to cases that match.

I create a date column using the row.names of your dataframe then have logical statements to define the age range with the label after.

To keep things tidy adding TRUE at the end with a label like "OTHER" will catch any results that do not match the conditions.

My example uses the dataset randu and I've added some random dates as the row names to give something similar to your image.

row.names(table) <- sample(seq(as.Date('2009/01/01'), as.Date('2019/12/31'), by="week"), 400)
table$date <- format(as.Date(row.names(table)), "%Y")

table <- table %>% mutate(
    
    date = case_when(
        
        date >= 2009 & date <= 2011
        ~ "2009-2011",
        
        date >= 2017 & date <= 2019
        ~ "2017-2019",
        
        TRUE ~ "OTHER"
    )
)

Upvotes: 0

Related Questions