从周和年中提取月份 [英] Extract Month from Week and Year number

查看:55
本文介绍了从周和年中提取月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,其中使用lubridate提取了星期和年份.我没有日期值了.

I have a dataframe where I extracted the week and year number using lubridate. I do not have the date values anymore.

我想从星期数和年数中提取月份.

I want to extract the month from the week number and year number.

df
Week    Year  
   1    2018
   5    2018
  45    2017

我希望我的最终输出是这样:

I want my final output to be this:

Week    Year  Month
   1    2018      1
   5    2018      2
  45    2017     11

推荐答案

这将提取一周中第一天的月份,如果需要,则可以添加一个常量或其他内容(如果需要的话是一周中的第二天或第三天.

This extracts the month of the first day of the week, you could add a constant or something if you wanted the 2nd or 3rd day of the week instead.

df %>% 
  mutate(Month = month(ymd(Year * 10000 + 0101) + Week * 7))

这篇关于从周和年中提取月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆