楼主: ddtpower3
17191 9

如何在R中获取日期中的周数 [推广有奖]

  • 0关注
  • 0粉丝

高中生

97%

还不是VIP/贵宾

-

威望
0
论坛币
87 个
通用积分
0.1889
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
229 点
帖子
13
精华
0
在线时间
65 小时
注册时间
2007-9-19
最后登录
2021-1-11

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
有一系列数据,跨了好几年,想按周进行汇聚,找出每周的中位数。请问R中有没有类似weekofyear的函数能够返回日期所在是一年中的第几周?
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Week year 中位数 KOF ear 获取

回帖推荐

谢益辉 发表于3楼  查看完整内容

?weekdayse.g. weekdays(as.Date('2007-10-31'))no need for custom functions :)

谢益辉 发表于4楼  查看完整内容

and use functions such as tapply() with median() to fulfill your idea (DO note there's still further work to do before tapply())

chitchatla 发表于5楼  查看完整内容

以下是引用谢益辉在2007-10-31 14:48:00的发言:?weekdayse.g. weekdays(as.Date('2007-10-31'))no need for custom functions :)I am not sure it is you or me. But one of us must have trouble understanding Chinese, English or R language...

本帖被以下文库推荐

沙发
chitchatla 发表于 2007-10-30 00:46:00 |只看作者 |坛友微信交流群

I wrote a quick function that you can use.

> weekofyear
function(current_date)
{
    t<-as.POSIXlt(current_date)
    t1<-ISOdate(t$year+1900, 1, 1)
    weeks<-difftime(current_date, t1, units="w")
    weeks
}
> weekofyear(as.Date("2007-10-28"))
Time difference of 42.78571 weeks

 

已有 1 人评分论坛币 学术水平 收起 理由
crystal8832 + 10 + 1 热心帮助其他会员

总评分: 论坛币 + 10  学术水平 + 1   查看全部评分

使用道具

藤椅
谢益辉 发表于 2007-10-31 14:48:00 |只看作者 |坛友微信交流群

?weekdays

e.g. weekdays(as.Date('2007-10-31'))

no need for custom functions :)

已有 1 人评分论坛币 学术水平 收起 理由
crystal8832 + 10 + 1 热心帮助其他会员

总评分: 论坛币 + 10  学术水平 + 1   查看全部评分

统计之都:http://cos.name 个人主页:http://yihui.name

使用道具

板凳
谢益辉 发表于 2007-10-31 14:52:00 |只看作者 |坛友微信交流群
and use functions such as tapply() with median() to fulfill your idea (DO note there's still further work to do before tapply())
统计之都:http://cos.name 个人主页:http://yihui.name

使用道具

报纸
chitchatla 发表于 2007-10-31 23:32:00 |只看作者 |坛友微信交流群
以下是引用谢益辉在2007-10-31 14:48:00的发言:

?weekdays

e.g. weekdays(as.Date('2007-10-31'))

no need for custom functions :)

I am not sure it is you or me. But one of us must have trouble understanding Chinese, English or R language...

使用道具

地板
谢益辉 发表于 2007-11-3 13:47:00 |只看作者 |坛友微信交流群

oh sorry, I misunderstood his words... but actually only a simple operator %/% is needed...

suppose there're n sample points, then (1:n)%/%7 or rep(1:(n%/%7), each=7) is approximately what he wanted (again, using tapply() & median())

统计之都:http://cos.name 个人主页:http://yihui.name

使用道具

7
xiaoman9527 发表于 2017-1-10 13:34:54 |只看作者 |坛友微信交流群
chitchatla 发表于 2007-10-30 00:46
I wrote a quick function that you can use.&gt; weekofyearfunction(current_date){&nbsp;&nbsp;&nbsp; t ...
这个算出来的和excel返回的好像有点不一样

使用道具

8
小朝 发表于 2017-6-9 11:47:13 |只看作者 |坛友微信交流群
  1. library(lubridate)
  2. paste0(year(Sys.Date()),formatC(week(Sys.Date()), flag = '0', width = 2))
复制代码
判断当前日期为哪一年的第几周

使用道具

9
猪香肠 发表于 2017-7-12 14:40:27 |只看作者 |坛友微信交流群
小朝 发表于 2017-6-9 11:47
判断当前日期为哪一年的第几周
非常感谢,正在查这个问题

使用道具

很简单的嘛!
> date<-as.Date("2022/1/20")
> date
[1] "2022-01-20"
> format(date,"%Y-%W")
[1] "2022-03"

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-9-21 08:54