字符串转换为数组:开放时间(一周以上)
我已经完成了 OSM 提取,在这里您可以看到 R 中每个对象的开放时间的“osm_openin”列。 它具有以下结构:
我希望一周中的每一天都有新的栏目,并带有符号“X” - 如果不是全天开放 - 或相应的开放时间日“07:00 - 21:00”。
我的解决方案:
首先,我考虑使用工作日的代表值“Mo = 1”,“Tu = 2”...“Su = 7”。如果日期/值本身没有明确提及,但以间隔形式存在,则这一点很重要。
对于每个值,我都在列中搜索其是否存在。 如果找到该值,我将直接获取随后的开放时间(不知道要使用哪个 R 命令) 如果不是,那么该值必须在一个区间内。例如“2”(星期二)不存在,那么脚本需要意识到星期二是在 Mo-Sa 之间。 (不知道该使用哪种方法)。
公共假期并不重要。
有什么解决方案的建议吗?
谢谢。
I've done an OSM-extraction and here you can see the column "osm_openin" for the opening hours for each object in R.
It has the following structure:
I would love to have new columns for each day of the week, with a symbol "X" - if it is not open all day - or the according opening hours for the day "07:00 - 21:00".
My solution:
Firstly, I am thinking of using representative values for the week days "Mo = 1", "Tu = 2"..."Su = 7". It is important, if the day/value itself is not explicitly mentioned, but is exisiting in an intervall.
For each value, I am searching its existence in the column.
If it finds the value, I'll take the opening hours following directly after (don't know which R command to use for that)
If not, then the value has to be in an intervall. For example "2" (Tuesday) is not existing, then the script needs to realize Tuesday is between Mo-Sa. (don't know which method to use for that).
Public Holiday is not important.
Any suggestion for a solution?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道最好的方法,但也许我可以为您提供帮助。
首先,我们需要创建工作日的数组:
现在让我们编写代码以将文本从
“ mo,we-fr”转换为vector
c(1,1,3,3,4,5) 。算法:“ pH”,“ SH”
);“ Mo”
- >1
,“ tu”
- >2
2 代码>等);-
用:
替换。例如,3-5
将为3:5
,它是R型代码;c(
添加到开始,)到结尾。例如,1,3:5
将为c(1,3:5)
;c(1,3:5)
是R风格向量,我们可以通过文本创建向量(eval(parse(parse(text =“ c(1,3:5)”)))< /代码>)。
完整代码:
让我们创建具有开放时间的功能(例如
“ Mo-Fr 6:30-19:00; SA 09:00-17:00; SU,pH 09:00-15:00”
)作为输入并返回数据。框架,带有7列(对于每个工作日)。算法:;
拆分文本;现在,我们将使用文本的一部分(例如,“ mo-fr 6:30-19:00”
);(space)拆分文本;
“ mo-fr 6:30-19:00”
- &gt;“ mo-fr”
和“ 6:30-19:00”
“ mo-fr”
),我们将> getwdays ,我们从第二部分制作矢量(大小将就像第一部分大小一样)。示例:“ mo-fr”
- &gt;c(1,2,3,4,5)
,“ 6:30-19:00”
- &gt;REP(“ 6:30-19:00”,5)
;bind_rows
。现在我们有了大数据。框架,但是某些工作日可能丢失了,并且某些工作日可能在time
中具有“ OFF”
;Merge
),然后替换“off
”和na
“ x”
((根据您的意愿);完整代码:
如果要应用
getTimetable
每行可以使用此代码:并且如果要将此data.frame添加到数据,则可以执行此类操作。 :
I don't know the best way, but may be I can help you.
Firstly we need to create array of weekdays:
Now let's write code for converting text from
"Mo,We-Fr"
to vectorc(1, 3, 4, 5)
. Algorithm:"PH", "SH"
);"Mo"
-->1
,"Tu"
-->2
, etc.);-
with:
. For example,3-5
will be3:5
and it is R-style code;c(
to the beginning and)
to the end. For example,1,3:5
will bec(1, 3:5)
;c(1, 3:5)
is R-style vector and we can create vector by text (eval(parse(text = "c(1, 3:5)"))
).Full code:
Let's create function that has opening hours (like
"Mo-Fr 6:30-19:00;Sa 09:00-17:00;Su,PH 09:00-15:00"
) as input and returns data.frame with 7 columns (for each weekday). Algorithm:;
; Now we will work with one part of text (for example,"Mo-Fr 6:30-19:00"
);(space);
"Mo-Fr 6:30-19:00"
-->"Mo-Fr"
and"6:30-19:00"
"Mo-Fr"
) we put intoGetWDays
and we make vector from second part (it's size will be like as first part size). Example:"Mo-Fr"
-->c(1,2,3,4,5)
,"6:30-19:00"
-->rep("6:30-19:00", 5)
;Day
andTime
);bind_rows
for each part from first step. Now we have big data.frame, but some weekdays may be missing, and some weekdays may have"Off"
in columnTime
;merge
) and replace "Off
" andNA
with"X"
(as you want);Full code:
If you want to apply
GetTimetable
for each row you can use this code:And if you want to add this data.frame to your data you can do something like this: