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

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

POSIXct objects problem into a R function

问题

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

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

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

英文:

I have the following part of a large dataset:

  1. structure(list(year = c(2020, 2020, 2020, 2020, 2020, 2020, 2020,
  2. 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
  3. 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
  4. 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
  5. 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
  6. 2020, 2020), month = c(11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  7. 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  8. 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  9. 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11), day = c(4, 4, 4,
  10. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  11. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  12. 4, 4, 4, 4, 4, 4, 4, 4), hour = c(11, 11, 11, 11, 11, 11, 11,
  13. 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 19, 19,
  14. 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20,
  15. 20, 20, 20, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23), min = c(3,
  16. 3, 4, 4, 4, 4, 4, 4, 4, 4, 34, 34, 34, 34, 34, 34, 34, 34, 34,
  17. 34, 34, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 8, 8, 8,
  18. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 38, 38, 38, 38), seg = c(58.91,
  19. 59.91, 0.91, 1.91, 2.91, 3.91, 4.91, 5.91, 6.91, 7.91, 28.85,
  20. 29.85, 30.85, 31.85, 32.85, 33.85, 34.85, 35.85, 36.85, 37.85,
  21. 38.85, 0.21, 1.21, 2.21, 3.21, 4.21, 5.21, 6.21, 7.21, 8.21,
  22. 9.21, 10.21, 6.52, 7.52, 8.52, 9.52, 10.52, 11.52, 12.52, 13.52,
  23. 14.52, 15.52, 57.28, 58.28, 59.28, 0.28, 1.28, 2.28, 3.28, 41.21,
  24. 42.21, 43.21, 44.21), Conductivity = c(56.257, 56.258, 56.256,
  25. 56.256, 56.267, 56.265, 56.265, 56.265, 56.262, 56.259, 56.291,
  26. 56.29, 56.291, 56.291, 56.292, 56.291, 56.289, 56.291, 56.29,
  27. 56.291, 56.291, 56.147, 56.146, 56.148, 56.148, 56.149, 56.149,
  28. 56.148, 56.148, 56.147, 56.149, 56.149, 56.146, 56.143, 56.143,
  29. 56.144, 56.144, 56.144, 56.145, 56.144, 56.147, 56.146, 55.956,
  30. 55.956, 55.955, 55.954, 55.952, 55.956, 55.957, 56.087, 56.086,
  31. 56.087, 56.085), Temperature = c(26.251, 26.252, 26.243, 26.249,
  32. 26.259, 26.259, 26.258, 26.26, 26.261, 26.251, 26.329, 26.329,
  33. 26.329, 26.33, 26.33, 26.33, 26.33, 26.33, 26.329, 26.329, 26.329,
  34. 26.226, 26.225, 26.226, 26.226, 26.226, 26.226, 26.226, 26.226,
  35. 26.226, 26.227, 26.226, 26.228, 26.227, 26.227, 26.227, 26.227,
  36. 26.227, 26.227, 26.227, 26.226, 26.228, 26.109, 26.109, 26.109,
  37. 26.109, 26.106, 26.109, 26.11, 26.311, 26.311, 26.31, 26.309),
  38. Pression = c(0.78, 0.82, 0.82, 0.82, 0.8, 0.8, 0.76, 0.78,
  39. 0.76, 0.77, 0.78, 0.78, 0.78, 0.8, 0.81, 0.8, 0.79, 0.79,
  40. 0.77, 0.78, 0.8, 0.77, 0.78, 0.79, 0.8, 0.8, 0.78, 0.78,
  41. 0.81, 0.78, 0.77, 0.79, 0.8, 0.8, 0.79, 0.8, 0.79, 0.79,
  42. 0.79, 0.76, 0.75, 0.76, 0.77, 0.77, 0.77, 0.74, 0.75, 0.76,
  43. 0.74, 0.78, 0.77, 0.79, 0.77), Chlorophyll = c(0.08, 0.08,
  44. 0.08, 0.08, 0.08, 0.07, 0.08, 0.08, 0.08, 0.09, 0.12, 0.14,
  45. 0.13, 0.14, 0.13, 0.13, 0.14, 0.13, 0.12, 0.14, 0.12, 0.11,
  46. 0.11, 0.11, 0.1, 0.12, 0.11, 0.12, 0.11, 0.12, 0.13, 0.11,
  47. 0.11, 0.1, 0.1, 0.09, 0.1, 0.1, 0.11, 0.1, 0.1, 0.1, 0.11,
  48. 0.1, 0.09, 0.09, 0.09, 0.1, 0.1, 0.12, 0.12, 0.1, 0.1), Turbidity = c(1.63,
  49. 1.64, 1.64, 1.64, 1.64, 1.64, 1.64, 1.64, 1.64, 1.64, 1.71,
  50. 1.71, 1.71, 1.71, 1.71, 1.71, 1.71, 1.72, 1.72, 1.72, 1.72,
  51. 1.51, 1.48, 1.45, 1.43, 1.39, 1.4, 1.4, 1.4, 1.38, 1.36,
  52. 1.34, 2.72, 2.61, 2.5, 2.4, 2.34, 2.26, 2.18, 2.09, 2.02,
  53. 2.02, 0.99, 1, 1, 1, 1, 1, 1, 0.79, 0.8, 0.8, 0.8), OD = c(229.4,
  54. 229.4, 229.4, 229.4, 229.4, 229.4, 229.4, 229.3, 229.3, 229.3,
  55. 224.2, 224.2, 224.2, 224.2, 224.2, 224.2, 224.2, 224.2, 224.2,
  56. 224.2, 224.2, 229.1, 229.1, 229.1, 229.1, 229.1, 229.2, 229.2,
  57. 229.2, 229.2, 229.2, 229.2, 228.5, 228.6, 228.6, 228.6, 228.5,
  58. 228.5, 228.5, 228.5, 228.5, 228.5, 217.9, 217.7, 217.7, 217.7,
  59. 217.7, 217.7, 217.7, 233.6, 233.5, 233.5, 233.5), Battery = c(7.95,
  60. 7.95, 7.97, 7.95, 7.97, 7.95, 7.95, 7.95, 7.95, 7.95, 7.95,
  61. 7.97, 7.95, 7.95, 7.95, 7.95, 7.95, 7.97, 7.95, 7.95, 7.95,
  62. 7.95, 7.95, 7.95, 7.97, 7.95, 7.97, 7.95, 7.95, 7.95, 7.97,
  63. 7.95, 7.95, 7.95, 7.95, 7.95, 7.95, 7.95, 7.95, 7.95, 7.97,
  64. 7.95, 7.95, 7.97, 7.95, 7.95, 7.95, 7.95, 7.95, 7.95, 7.95,
  65. 7.95, 7.95), Salinity = c(36.36765, 36.36758, 36.37325, 36.36849,
  66. 36.3686, 36.36714, 36.36795, 36.36636, 36.36338, 36.36911,
  67. 36.33071, 36.32998, 36.33071, 36.32991, 36.33064, 36.32991,
  68. 36.32846, 36.32991, 36.32998, 36.33071, 36.3307, 36.30722,
  69. 36.30728, 36.30795, 36.30794, 36.30867, 36.30868, 36.30795,
  70. 36.30794, 36.30722, 36.30789, 36.30868, 36.3049, 36.30351,
  71. 36.30351, 36.30424, 36.30424, 36.30424, 36.30497, 36.30425,
  72. 36.30723, 36.30492, 36.26034, 36.26034, 36.25961, 36.25889,
  73. 36.2598, 36.26035, 36.26029, 36.19642, 36.19569, 36.1972,
  74. 36.19654)), row.names = c(NA, -53L), class = c(&quot;tbl_df&quot;,
  75. &quot;tbl&quot;, &quot;data.frame&quot;))

And I build the following function:

  1. plot_amb2 &lt;- function(data, amb_var, time_scale,
  2. xlab, ylab, ybreak, xbreak,
  3. cor, leglab, quant_range) {
  4. df &lt;- tab_ambi_laps %&gt;%
  5. mutate(date_time = make_datetime(year, month, day, hour, min, seg)) %&gt;%
  6. mutate(cycle_rounded = round_date(date_time, &quot;30 min&quot;)) %&gt;%
  7. filter(!!ensym(amb_var) &gt;= quantile(!!ensym(amb_var),
  8. quant_range[1], na.rm = T) &amp;
  9. !!ensym(amb_var) &lt;= quantile(!!ensym(amb_var),
  10. quant_range[2], na.rm = T))
  11. cycles &lt;- tibble(cycle_rounded = seq(ymd_hm(&quot;2020-11-01 0:00&quot;),
  12. ymd_hm(&quot;2022-06-10 23:59&quot;),
  13. by = &quot;30 min&quot;))
  14. df &lt;- full_join(df, cycles)
  15. df &lt;- df %&gt;%
  16. mutate(cycle_rounded = as.POSIXct(cycle_rounded)) %&gt;%
  17. group_by(Datetime = round_date(cycle_rounded, time_scale)) %&gt;%
  18. summarise_at(vars(!!ensym(amb_var)), mean, na.rm = TRUE) %&gt;%
  19. mutate(date_ = if_else(is.na(Datetime), NA, format(Datetime, &quot;%Y-%m-%d&quot;) %&gt;% ymd()),
  20. time_ = if_else(is.na(Datetime), NA, format(Datetime, &quot;%H:%M:%S&quot;) %&gt;% as_hms),
  21. amb_var := if_else(is.nan(!!ensym(amb_var)), NA, !!ensym(amb_var))) %&gt;%
  22. drop_na()
  23. lims2 &lt;- as.Date(c(&quot;2020-11-01&quot;,&quot;2022-06-10&quot;))
  24. ggplot(data = df) +
  25. geom_raster(aes(x = date_, y = time_, fill = !!ensym(amb_var))) +
  26. scale_fill_gradientn(colours = cor, na.value = &quot;white&quot;) +
  27. scale_y_datetime(date_breaks = ybreak, date_labels = &quot;%H:%M&quot;) +
  28. scale_x_datetime(date_labels = &quot;%m-%Y&quot;, breaks = xbreak,
  29. limits = as.POSIXct(lims2)) +
  30. labs(y = ylab, x = xlab, fill = leglab) +
  31. theme_test() +
  32. theme(axis.text.x = element_text(size = 14, angle = 45,
  33. vjust = 1, hjust = 1,
  34. color = &quot;black&quot;, face = &quot;bold&quot;),
  35. axis.text.y = element_text(size = 14,
  36. color = &quot;black&quot;, face = &quot;bold&quot;),
  37. axis.title.x = element_text(size = 16,
  38. color = &quot;black&quot;, face = &quot;bold&quot;),
  39. axis.title.y = element_text(size = 16,
  40. color = &quot;black&quot;, face = &quot;bold&quot;),
  41. legend.text = element_text(size = 14, face = &quot;bold&quot;),
  42. legend.title = element_text(size = 16, face = &quot;bold&quot;))
  43. }

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

  1. plot_amb2(data = tab_ambi_laps, amb_var = Chlorophyll,
  2. time_scale = &quot;1 hour&quot;, quant_range = c(0.15, 0.95),
  3. xlab = &quot;Data&quot;, ylab = &quot;Hora&quot;, leglab = &quot;Clorofila\n(\u00B5g/L)&quot;,
  4. ybreak = &quot;3 hours&quot;, xbreak = &quot;2 month&quot;,
  5. 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,那么这段代码就可以正常工作:

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

并且让x轴使用Date数据:

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

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

英文:

Works for me if I define time_ as a datetime:

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

and make the x axis use Date data:

  1. scale_y_datetime(date_breaks = ybreak, date_labels = &quot;%H:%M&quot;) +
  2. scale_x_date(date_labels = &quot;%m-%Y&quot;, breaks = xbreak,
  3. 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:

确定