Extract Year and Month from yearmon Object in R | zoo & lubridate Packages | Using format() Function

preview_player
Показать описание
R code of this video:

library("zoo")

my_date # Print example date

class(my_date) # Check class of example date

format(my_date, "%Y") # Extracting year

format(my_date, "%b") # Extracting month as name

format(my_date, "%m") # Extracting month as character number

library("lubridate")

year(my_date) # Applying year function

month(my_date) # Applying month function

Follow me on Social Media:
Рекомендации по теме
Комментарии
Автор

The best R tutor online!! Your channel should be 10x its size IMHO!

Without your content i might have given up learning R. Thank you! 😊

BridgeTROLL
Автор

I executed the following steps, “date” column with in a yyyy-dd-mm format and a chr datatype. I converted that column to numeric the created a new “year” column with df$year = as.numeric(format(df$year, “%Y”)). I successfully created the new column but all the values were NAs. Is there a better way?

frankjr