字符串转换为数组:开放时间(一周以上)

发布于 2025-01-18 04:48:58 字数 560 浏览 2 评论 0原文

我已经完成了 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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沒落の蓅哖 2025-01-25 04:48:58

我不知道最好的方法,但也许我可以为您提供帮助。
首先,我们需要创建工作日的数组:

wdays <- c("Mo", "Tu", "We", "Th", "Fr", "Sa", "Su")

现在让我们编写代码以将文本从“ mo,we-fr”转换为vector c(1,1,3,3,4,5) 。算法:

  1. 删除有关节日的信息(“ pH”,“ SH”);
  2. 用数字替换工作日的名称(“ Mo” - &gt; 1“ tu” - &gt; 2 2 代码>等);
  3. -替换。例如,3-5将为3:5,它是R型代码;
  4. c(添加到开始,)到结尾。例如,1,3:5将为c(1,3:5);
  5. c(1,3:5)是R风格向量,我们可以通过文本创建向量(eval(parse(parse(text =“ c(1,3:5)”)))< /代码>)。

完整代码:

GetWDays <- function(x, wdays) {
    holi <- c("PH", "SH")
    x <- gsub(paste0("(,|^)", holi, collapse = "|"), "", x) #delete holidays
    
    for (i in 1:7) {
        x <- gsub(wdays[i], i, x)
    }
    
    x <- gsub("-", ":", x)
    x <- paste0("c(", x, ")")
    
    wday_idx <- eval(parse(text = x))
    return(wday_idx)
}

让我们创建具有开放时间的功能(例如“ Mo-Fr 6:30-19:00; SA 09:00-17:00; SU,pH 09:00-15:00”)作为输入并返回数据。框架,带有7列(对于每个工作日)。算法:

  1. 通过;拆分文本;现在,我们将使用文本的一部分(例如,“ mo-fr 6:30-19:00”);
  2. 通过(space)拆分文本; “ mo-fr 6:30-19:00” - &gt; “ mo-fr”“ 6:30-19:00”
  3. 第一部分(“ mo-fr”),我们将> getwdays ,我们从第二部分制作矢量(大小将就像第一部分大小一样)。示例:“ mo-fr” - &gt; c(1,2,3,4,5)“ 6:30-19:00” - &gt; REP(“ 6:30-19:00”,5);
  4. 从2个向量制作数据。
  5. 从第一步使用bind_rows。现在我们有了大数据。框架,但是某些工作日可能丢失了,并且某些工作日可能在time中具有“ OFF”;
  6. 因此,添加缺少工作日的行(通过Merge),然后替换“ off”和na “ x”((根据您的意愿);
  7. transpose data.frame并返回

完整代码:

GetTimetable <- function(x) {
    wdays <- c("Mo", "Tu", "We", "Th", "Fr", "Sa", "Su")
    
    
    tmp <- strsplit(strsplit(x, ";")[[1]], " ")
    tmp <- lapply(tmp, function(x) {Day <- GetWDays(x[1], wdays); data.frame(Day, Time = rep(x[2], length(Day)))})
    tmp <- bind_rows(tmp) %>% arrange(Day) %>% as.data.frame()
    tmp <- merge(data.frame(Day = 1:7), tmp, all.x = T, by = "Day")
    tmp$Time[is.na(tmp$Time) | tmp$Time == "Off"] = "X"
    
    tmp <- tmp %>% t() %>% "["(2, ) %>% as.list() %>% setNames(wdays) %>% bind_cols()
    return(tmp)
}

如果要应用getTimetable每行可以使用此代码:

df_time <- df$osm_openning %>% lapply(GetTimetable) %>% bind_rows()

并且如果要将此data.frame添加到数据,则可以执行此类操作。 :

df <- bind_cols(df, df_time)

I don't know the best way, but may be I can help you.
Firstly we need to create array of weekdays:

wdays <- c("Mo", "Tu", "We", "Th", "Fr", "Sa", "Su")

Now let's write code for converting text from "Mo,We-Fr" to vector c(1, 3, 4, 5). Algorithm:

  1. Delete information about holidays ("PH", "SH");
  2. Replace name of weekday with number ("Mo" --> 1, "Tu" --> 2, etc.);
  3. Replace - with :. For example, 3-5 will be 3:5 and it is R-style code;
  4. Add c( to the beginning and ) to the end. For example, 1,3:5 will be c(1, 3:5);
  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:

GetWDays <- function(x, wdays) {
    holi <- c("PH", "SH")
    x <- gsub(paste0("(,|^)", holi, collapse = "|"), "", x) #delete holidays
    
    for (i in 1:7) {
        x <- gsub(wdays[i], i, x)
    }
    
    x <- gsub("-", ":", x)
    x <- paste0("c(", x, ")")
    
    wday_idx <- eval(parse(text = x))
    return(wday_idx)
}

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:

  1. Split text by ;; Now we will work with one part of text (for example, "Mo-Fr 6:30-19:00");
  2. Split text by (space); "Mo-Fr 6:30-19:00" --> "Mo-Fr" and "6:30-19:00"
  3. First part ("Mo-Fr") we put into GetWDays 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);
  4. Make data.frame from 2 vectors (Day and Time);
  5. Use 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 column Time;
  6. So add rows for missing weekdays (by merge) and replace "Off" and NA with "X" (as you want);
  7. Transpose data.frame and return

Full code:

GetTimetable <- function(x) {
    wdays <- c("Mo", "Tu", "We", "Th", "Fr", "Sa", "Su")
    
    
    tmp <- strsplit(strsplit(x, ";")[[1]], " ")
    tmp <- lapply(tmp, function(x) {Day <- GetWDays(x[1], wdays); data.frame(Day, Time = rep(x[2], length(Day)))})
    tmp <- bind_rows(tmp) %>% arrange(Day) %>% as.data.frame()
    tmp <- merge(data.frame(Day = 1:7), tmp, all.x = T, by = "Day")
    tmp$Time[is.na(tmp$Time) | tmp$Time == "Off"] = "X"
    
    tmp <- tmp %>% t() %>% "["(2, ) %>% as.list() %>% setNames(wdays) %>% bind_cols()
    return(tmp)
}

If you want to apply GetTimetable for each row you can use this code:

df_time <- df$osm_openning %>% lapply(GetTimetable) %>% bind_rows()

And if you want to add this data.frame to your data you can do something like this:

df <- bind_cols(df, df_time)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文