R 更快的工作日方法
您好,我正在尝试将给定的日期转换为各自的工作日,我有一个百万行的数据集,并且我仅使用其中包含日期的列。
我目前正在使用
ifelse(wday(data$started_at)==1,7,wday(data$started_at)-1)
“我希望星期一显示为 1,星期日显示为 7”,但是,我并不在意,我宁愿有一个更快的程序。
作为您使用的试用数据:
x<- rep("2022-02-01 00:00:04",1000000)
这是我目前拥有的数据,
ifelse(wday(x)==1,7,wday(x)-1)
我正在努力使其更快,目前在我的计算机上需要 17 秒。
Hi I am trying to convert the given dates into their respective weekdays, I have a data set of million lines, and I am only using the column with the dates in it.
I am currently using
ifelse(wday(data$started_at)==1,7,wday(data$started_at)-1)
I want Monday to be indicated as 1 and Sunday as 7, however, I do not really care, I would much rather have a faster program.
As trial data you use :
x<- rep("2022-02-01 00:00:04",1000000)
This is what I currently have
ifelse(wday(x)==1,7,wday(x)-1)
I am trying to make it much faster, it currently takes 17second on my computer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
lubridate::wday
中有一个名为week_start
的参数:There is an argument in
lubridate::wday
calledweek_start
:这里不需要额外的
ifelse
和调用wday
两次。奇怪的是,下面的代码稍微快一些,并给出一个整数,而不是wday
中的默认数字。The extra
ifelse
and callingwday
twice is not needed here. Curiously, the following is slightly faster, and gives an integer instead of the default numeric fromwday
.