全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 R语言论坛
17543 9
2007-10-23
有一系列数据,跨了好几年,想按周进行汇聚,找出每周的中位数。请问R中有没有类似weekofyear的函数能够返回日期所在是一年中的第几周?
二维码

扫码加我 拉你入群

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

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

全部回复
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

 

二维码

扫码加我 拉你入群

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

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

2007-10-31 14:48:00

?weekdays

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

no need for custom functions :)

二维码

扫码加我 拉你入群

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

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

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())
二维码

扫码加我 拉你入群

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

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

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())

二维码

扫码加我 拉你入群

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

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

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群