mardi 23 novembre 2021

Conditionally formatting a column with mutate and regex in R

I'm brand new in R and programming in general. I have a column containing a list of dates. Some are in the "01 January 2020" format, some have only month and year (ie "January 2020" only). I want to mutate them to a new field where I add a 01 in front of all the dates that are in the month year format, and then I will use lubridate to process it into dates

This is what I've tried. I'm trying to extract the first character of the Date column. If it is an upper case letter, then I will append "01" to it. I am using the tinyverse package including dplyr

df %>% mutate(new_date = ifelse(str_sub(Date, start = 1, end = 1)== "[:upper:]"), paste('01', Date, sep = ' '), new_date = Date)

I'm getting the error message "no is missing", but I thought I have included new_date = Date to keep the current formatting.

Thank you for your help!

Aucun commentaire:

Enregistrer un commentaire