使用现有的“日期”变量;将月和周变量添加到数据框

Using existing “Date” variable; add month and week variables to the data frame

我有一个名为 Covid19 的数据框,它包含一个名为 Date 的变量。我想从 Date 变量创建两个新变量 Month 和 Week。 The output must look as in the image 我写的代码是:

Covid19_df <- Covid19_df$Date %>% mutate(Month = lubridate::month(date), 
                Week = lubridate::week(date))

但是报错

Error in UseMethod("mutate_") : 
  no applicable method for 'mutate_' applied to an object of class "Date"

任何人都可以给我一个可能的解决方案或新代码获得所需的输出。

您要查找的语法是:

library(dplyr)

Covid19_df <- Covid19_df %>% 
                mutate(Month = lubridate::month(Date), 
                        Week = lubridate::week(Date))