将POSIXct对象问题嵌入到R函数中。

huangapple go评论61阅读模式
英文:

POSIXct objects problem into a R function

问题

抱歉,您遇到了一个时间格式的问题。要解决这个问题,您可以在函数内部确保将时间列(Datetime)的数据类型转换为 POSIXct 类型。请在函数的以下部分添加这一行代码,将 Datetime 列转换为 POSIXct 类型:

df <- df %>%
  mutate(Datetime = as.POSIXct(Datetime))

这样,您的时间列就会被正确地转换为 POSIXct 类型,然后您的函数应该能够正常运行,不再出现时间格式的错误。希望这可以帮助您解决问题。如果您还有其他问题,请随时提出。

英文:

I have the following part of a large dataset:

structure(list(year = c(2020, 2020, 2020, 2020, 2020, 2020, 2020, 
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
2020, 2020), month = c(11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 
11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 
11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 
11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11), day = c(4, 4, 4, 
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
4, 4, 4, 4, 4, 4, 4, 4), hour = c(11, 11, 11, 11, 11, 11, 11, 
11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 19, 19, 
19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 
20, 20, 20, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23), min = c(3, 
3, 4, 4, 4, 4, 4, 4, 4, 4, 34, 34, 34, 34, 34, 34, 34, 34, 34, 
34, 34, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 8, 8, 8, 
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 38, 38, 38, 38), seg = c(58.91, 
59.91, 0.91, 1.91, 2.91, 3.91, 4.91, 5.91, 6.91, 7.91, 28.85, 
29.85, 30.85, 31.85, 32.85, 33.85, 34.85, 35.85, 36.85, 37.85, 
38.85, 0.21, 1.21, 2.21, 3.21, 4.21, 5.21, 6.21, 7.21, 8.21, 
9.21, 10.21, 6.52, 7.52, 8.52, 9.52, 10.52, 11.52, 12.52, 13.52, 
14.52, 15.52, 57.28, 58.28, 59.28, 0.28, 1.28, 2.28, 3.28, 41.21, 
42.21, 43.21, 44.21), Conductivity = c(56.257, 56.258, 56.256, 
56.256, 56.267, 56.265, 56.265, 56.265, 56.262, 56.259, 56.291, 
56.29, 56.291, 56.291, 56.292, 56.291, 56.289, 56.291, 56.29, 
56.291, 56.291, 56.147, 56.146, 56.148, 56.148, 56.149, 56.149, 
56.148, 56.148, 56.147, 56.149, 56.149, 56.146, 56.143, 56.143, 
56.144, 56.144, 56.144, 56.145, 56.144, 56.147, 56.146, 55.956, 
55.956, 55.955, 55.954, 55.952, 55.956, 55.957, 56.087, 56.086, 
56.087, 56.085), Temperature = c(26.251, 26.252, 26.243, 26.249, 
26.259, 26.259, 26.258, 26.26, 26.261, 26.251, 26.329, 26.329, 
26.329, 26.33, 26.33, 26.33, 26.33, 26.33, 26.329, 26.329, 26.329, 
26.226, 26.225, 26.226, 26.226, 26.226, 26.226, 26.226, 26.226, 
26.226, 26.227, 26.226, 26.228, 26.227, 26.227, 26.227, 26.227, 
26.227, 26.227, 26.227, 26.226, 26.228, 26.109, 26.109, 26.109, 
26.109, 26.106, 26.109, 26.11, 26.311, 26.311, 26.31, 26.309), 
    Pression = c(0.78, 0.82, 0.82, 0.82, 0.8, 0.8, 0.76, 0.78, 
    0.76, 0.77, 0.78, 0.78, 0.78, 0.8, 0.81, 0.8, 0.79, 0.79, 
    0.77, 0.78, 0.8, 0.77, 0.78, 0.79, 0.8, 0.8, 0.78, 0.78, 
    0.81, 0.78, 0.77, 0.79, 0.8, 0.8, 0.79, 0.8, 0.79, 0.79, 
    0.79, 0.76, 0.75, 0.76, 0.77, 0.77, 0.77, 0.74, 0.75, 0.76, 
    0.74, 0.78, 0.77, 0.79, 0.77), Chlorophyll = c(0.08, 0.08, 
    0.08, 0.08, 0.08, 0.07, 0.08, 0.08, 0.08, 0.09, 0.12, 0.14, 
    0.13, 0.14, 0.13, 0.13, 0.14, 0.13, 0.12, 0.14, 0.12, 0.11, 
    0.11, 0.11, 0.1, 0.12, 0.11, 0.12, 0.11, 0.12, 0.13, 0.11, 
    0.11, 0.1, 0.1, 0.09, 0.1, 0.1, 0.11, 0.1, 0.1, 0.1, 0.11, 
    0.1, 0.09, 0.09, 0.09, 0.1, 0.1, 0.12, 0.12, 0.1, 0.1), Turbidity = c(1.63, 
    1.64, 1.64, 1.64, 1.64, 1.64, 1.64, 1.64, 1.64, 1.64, 1.71, 
    1.71, 1.71, 1.71, 1.71, 1.71, 1.71, 1.72, 1.72, 1.72, 1.72, 
    1.51, 1.48, 1.45, 1.43, 1.39, 1.4, 1.4, 1.4, 1.38, 1.36, 
    1.34, 2.72, 2.61, 2.5, 2.4, 2.34, 2.26, 2.18, 2.09, 2.02, 
    2.02, 0.99, 1, 1, 1, 1, 1, 1, 0.79, 0.8, 0.8, 0.8), OD = c(229.4, 
    229.4, 229.4, 229.4, 229.4, 229.4, 229.4, 229.3, 229.3, 229.3, 
    224.2, 224.2, 224.2, 224.2, 224.2, 224.2, 224.2, 224.2, 224.2, 
    224.2, 224.2, 229.1, 229.1, 229.1, 229.1, 229.1, 229.2, 229.2, 
    229.2, 229.2, 229.2, 229.2, 228.5, 228.6, 228.6, 228.6, 228.5, 
    228.5, 228.5, 228.5, 228.5, 228.5, 217.9, 217.7, 217.7, 217.7, 
    217.7, 217.7, 217.7, 233.6, 233.5, 233.5, 233.5), Battery = c(7.95, 
    7.95, 7.97, 7.95, 7.97, 7.95, 7.95, 7.95, 7.95, 7.95, 7.95, 
    7.97, 7.95, 7.95, 7.95, 7.95, 7.95, 7.97, 7.95, 7.95, 7.95, 
    7.95, 7.95, 7.95, 7.97, 7.95, 7.97, 7.95, 7.95, 7.95, 7.97, 
    7.95, 7.95, 7.95, 7.95, 7.95, 7.95, 7.95, 7.95, 7.95, 7.97, 
    7.95, 7.95, 7.97, 7.95, 7.95, 7.95, 7.95, 7.95, 7.95, 7.95, 
    7.95, 7.95), Salinity = c(36.36765, 36.36758, 36.37325, 36.36849, 
    36.3686, 36.36714, 36.36795, 36.36636, 36.36338, 36.36911, 
    36.33071, 36.32998, 36.33071, 36.32991, 36.33064, 36.32991, 
    36.32846, 36.32991, 36.32998, 36.33071, 36.3307, 36.30722, 
    36.30728, 36.30795, 36.30794, 36.30867, 36.30868, 36.30795, 
    36.30794, 36.30722, 36.30789, 36.30868, 36.3049, 36.30351, 
    36.30351, 36.30424, 36.30424, 36.30424, 36.30497, 36.30425, 
    36.30723, 36.30492, 36.26034, 36.26034, 36.25961, 36.25889, 
    36.2598, 36.26035, 36.26029, 36.19642, 36.19569, 36.1972, 
    36.19654)), row.names = c(NA, -53L), class = c(&quot;tbl_df&quot;, 
&quot;tbl&quot;, &quot;data.frame&quot;))

And I build the following function:

plot_amb2 &lt;- function(data, amb_var, time_scale, 
                      xlab, ylab, ybreak, xbreak, 
                      cor, leglab, quant_range) {
  
  df &lt;- tab_ambi_laps %&gt;% 
    mutate(date_time = make_datetime(year, month, day, hour, min, seg)) %&gt;% 
    mutate(cycle_rounded = round_date(date_time, &quot;30 min&quot;)) %&gt;% 
    filter(!!ensym(amb_var) &gt;= quantile(!!ensym(amb_var), 
                                        quant_range[1], na.rm = T) &amp; 
             !!ensym(amb_var) &lt;= quantile(!!ensym(amb_var), 
                                          quant_range[2], na.rm = T))
  
  cycles &lt;- tibble(cycle_rounded = seq(ymd_hm(&quot;2020-11-01 0:00&quot;),
                                       ymd_hm(&quot;2022-06-10 23:59&quot;),
                                       by = &quot;30 min&quot;))
  
  df &lt;- full_join(df, cycles)
  
  df &lt;- df %&gt;%
    mutate(cycle_rounded = as.POSIXct(cycle_rounded)) %&gt;%
    group_by(Datetime = round_date(cycle_rounded, time_scale)) %&gt;% 
    summarise_at(vars(!!ensym(amb_var)), mean, na.rm = TRUE) %&gt;%
    mutate(date_ = if_else(is.na(Datetime), NA, format(Datetime, &quot;%Y-%m-%d&quot;) %&gt;% ymd()),
           time_ = if_else(is.na(Datetime), NA, format(Datetime, &quot;%H:%M:%S&quot;) %&gt;% as_hms),
           amb_var := if_else(is.nan(!!ensym(amb_var)), NA, !!ensym(amb_var))) %&gt;% 
    drop_na()
  
  lims2 &lt;- as.Date(c(&quot;2020-11-01&quot;,&quot;2022-06-10&quot;))
  
  ggplot(data = df) + 
    geom_raster(aes(x = date_, y = time_, fill = !!ensym(amb_var))) + 
    scale_fill_gradientn(colours = cor, na.value = &quot;white&quot;) +
    scale_y_datetime(date_breaks = ybreak, date_labels = &quot;%H:%M&quot;) +
    scale_x_datetime(date_labels = &quot;%m-%Y&quot;, breaks = xbreak, 
                     limits = as.POSIXct(lims2)) +
    labs(y = ylab, x = xlab, fill = leglab) +
    theme_test() +
    theme(axis.text.x = element_text(size = 14, angle = 45, 
                                     vjust = 1, hjust = 1,
                                     color = &quot;black&quot;, face = &quot;bold&quot;),
          axis.text.y = element_text(size = 14,
                                     color = &quot;black&quot;, face = &quot;bold&quot;), 
          axis.title.x = element_text(size = 16,
                                      color = &quot;black&quot;, face = &quot;bold&quot;), 
          axis.title.y = element_text(size = 16,
                                      color = &quot;black&quot;, face = &quot;bold&quot;),
          legend.text = element_text(size = 14, face = &quot;bold&quot;),
          legend.title = element_text(size = 16, face = &quot;bold&quot;))
}

However, when I run this function on the following way:

plot_amb2(data = tab_ambi_laps, amb_var = Chlorophyll, 
          time_scale = &quot;1 hour&quot;, quant_range = c(0.15, 0.95),
          xlab = &quot;Data&quot;, ylab = &quot;Hora&quot;, leglab = &quot;Clorofila\n(\u00B5g/L)&quot;,
          ybreak = &quot;3 hours&quot;, xbreak = &quot;2 month&quot;,
          cor = c(&quot;lightblue&quot;, &quot;yellow&quot;, &quot;darkred&quot;))

I have this error message:

> Error: Invalid input: time_trans works with objects of class POSIXct
> only

Apparently, I have a problem with the time format of my function.
When I run these parts of codes separately from my function its works well. But I need to incorporate everything into a function.

How can I solve this?

Thanks of any help

答案1

得分: 1

只返回翻译好的部分:

如果我将time_定义为一个datetime,那么这段代码就可以正常工作:

time_ = if_else(is.na(Datetime), NA, 
               make_datetime(hour = hour(Datetime), 
                             min = minute(Datetime), 
                             sec = second(Datetime))),

并且让x轴使用Date数据:

scale_y_datetime(date_breaks = ybreak, date_labels = "%H:%M") +
scale_x_date(date_labels = "%m-%Y", breaks = xbreak, limits = lims2)

可以推测,如果使用完整的数据,这看起来会更有趣。这个示例只反映了跨多年的一个日期的一小部分。

英文:

Works for me if I define time_ as a datetime:

time_ = if_else(is.na(Datetime), NA, 
                           make_datetime(hour = hour(Datetime), 
                                         min = minute(Datetime), 
                                         sec = second(Datetime))),

and make the x axis use Date data:

scale_y_datetime(date_breaks = ybreak, date_labels = &quot;%H:%M&quot;) +
scale_x_date(date_labels = &quot;%m-%Y&quot;, breaks = xbreak,
                 limits = lims2) +

Presumably this looks more interesting with your full data. The sample only reflects a small slice of one date across multiple years.

将POSIXct对象问题嵌入到R函数中。

huangapple
  • 本文由 发表于 2023年6月22日 02:10:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526055.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定