如何从日期列中提取月份和年份并为每个列再添加两列
How to extract Month and Year from Date column and add two more columns for each
我目前正在处理一个数据框,它是从一个包含“日期”列的 .csv 文件导入的:
First Image
然后我想使用以下代码将“日期”列中的月份和年份提取到两个新列中,分别用于月份和年份:
act_weather_data["Month"] <- format(as.Date(act_weather_data$Date), "%m") ## For Month
act_weather_data["Year"] <- format(as.Date(act_weather_data$Date), "%Y") ## For Year
以上代码有效,但是 Year 列似乎显示不正确:
Second Image
似乎“年份”列使用的是日期,而不是“日期”列中可以看到的实际年份。
我不确定为什么“年”列会这样显示。谁能帮我解决这个问题?非常感谢!
看起来它是抓住了一天而不是一年。因此,您的日期在通过 as.Date() 函数
时似乎格式不正确
看看这是什么
as.Date(act_weather_data$Date)
看起来很像并相应地格式化
即
as.Date(act_weather_data$Date, format="%Y/%m/%d")
然后apply.the像以前一样格式化
即
Year=format(as.Date(act_weather_data$Date, format="%Y/%m/%d"),"%Y")
对于下面的解决方案,需要安装两个包:dplyr
和 lubridate
。
# Install the necessary packages
install.packages("dplyr")
install.packages("lubridate")
# Load the packages
library(dplyr)
library(lubridate)
# create dates dataframe
dates_dt <- data.frame(the_dates=seq(as.Date('2022-01-01'),
as.Date('2022-01-10'),
by='days'))
# Look at the dataframe
dates_dt
# Double check they are actually dates
class(dates_dt$the_dates)
# Extract the month
lubridate::month(dates_dt$the_dates)
# Extract the year
lubridate::year(dates_dt$the_dates)
# Perhaps you want the month name instead? no problem
month.name[lubridate::month(dates_dt$the_dates)]
# Now add a column for each
dates_dt <- dates_dt %>%
mutate(year=lubridate::year(dates_dt$the_dates),
month=lubridate::month(dates_dt$the_dates),
month_name=month.name[lubridate::month(dates_dt$the_dates)])
# Have a look at the result
dates_dt
希望您觉得它有用。祝您在 R 中编码愉快!
我目前正在处理一个数据框,它是从一个包含“日期”列的 .csv 文件导入的: First Image
然后我想使用以下代码将“日期”列中的月份和年份提取到两个新列中,分别用于月份和年份:
act_weather_data["Month"] <- format(as.Date(act_weather_data$Date), "%m") ## For Month
act_weather_data["Year"] <- format(as.Date(act_weather_data$Date), "%Y") ## For Year
以上代码有效,但是 Year 列似乎显示不正确: Second Image
似乎“年份”列使用的是日期,而不是“日期”列中可以看到的实际年份。 我不确定为什么“年”列会这样显示。谁能帮我解决这个问题?非常感谢!
看起来它是抓住了一天而不是一年。因此,您的日期在通过 as.Date() 函数
时似乎格式不正确看看这是什么
as.Date(act_weather_data$Date)
看起来很像并相应地格式化
即
as.Date(act_weather_data$Date, format="%Y/%m/%d")
然后apply.the像以前一样格式化 即
Year=format(as.Date(act_weather_data$Date, format="%Y/%m/%d"),"%Y")
对于下面的解决方案,需要安装两个包:dplyr
和 lubridate
。
# Install the necessary packages
install.packages("dplyr")
install.packages("lubridate")
# Load the packages
library(dplyr)
library(lubridate)
# create dates dataframe
dates_dt <- data.frame(the_dates=seq(as.Date('2022-01-01'),
as.Date('2022-01-10'),
by='days'))
# Look at the dataframe
dates_dt
# Double check they are actually dates
class(dates_dt$the_dates)
# Extract the month
lubridate::month(dates_dt$the_dates)
# Extract the year
lubridate::year(dates_dt$the_dates)
# Perhaps you want the month name instead? no problem
month.name[lubridate::month(dates_dt$the_dates)]
# Now add a column for each
dates_dt <- dates_dt %>%
mutate(year=lubridate::year(dates_dt$the_dates),
month=lubridate::month(dates_dt$the_dates),
month_name=month.name[lubridate::month(dates_dt$the_dates)])
# Have a look at the result
dates_dt
希望您觉得它有用。祝您在 R 中编码愉快!