英文:
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("tbl_df",
"tbl", "data.frame"))
And I build the following function:
plot_amb2 <- function(data, amb_var, time_scale,
xlab, ylab, ybreak, xbreak,
cor, leglab, quant_range) {
df <- tab_ambi_laps %>%
mutate(date_time = make_datetime(year, month, day, hour, min, seg)) %>%
mutate(cycle_rounded = round_date(date_time, "30 min")) %>%
filter(!!ensym(amb_var) >= quantile(!!ensym(amb_var),
quant_range[1], na.rm = T) &
!!ensym(amb_var) <= quantile(!!ensym(amb_var),
quant_range[2], na.rm = T))
cycles <- tibble(cycle_rounded = seq(ymd_hm("2020-11-01 0:00"),
ymd_hm("2022-06-10 23:59"),
by = "30 min"))
df <- full_join(df, cycles)
df <- df %>%
mutate(cycle_rounded = as.POSIXct(cycle_rounded)) %>%
group_by(Datetime = round_date(cycle_rounded, time_scale)) %>%
summarise_at(vars(!!ensym(amb_var)), mean, na.rm = TRUE) %>%
mutate(date_ = if_else(is.na(Datetime), NA, format(Datetime, "%Y-%m-%d") %>% ymd()),
time_ = if_else(is.na(Datetime), NA, format(Datetime, "%H:%M:%S") %>% as_hms),
amb_var := if_else(is.nan(!!ensym(amb_var)), NA, !!ensym(amb_var))) %>%
drop_na()
lims2 <- as.Date(c("2020-11-01","2022-06-10"))
ggplot(data = df) +
geom_raster(aes(x = date_, y = time_, fill = !!ensym(amb_var))) +
scale_fill_gradientn(colours = cor, na.value = "white") +
scale_y_datetime(date_breaks = ybreak, date_labels = "%H:%M") +
scale_x_datetime(date_labels = "%m-%Y", 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 = "black", face = "bold"),
axis.text.y = element_text(size = 14,
color = "black", face = "bold"),
axis.title.x = element_text(size = 16,
color = "black", face = "bold"),
axis.title.y = element_text(size = 16,
color = "black", face = "bold"),
legend.text = element_text(size = 14, face = "bold"),
legend.title = element_text(size = 16, face = "bold"))
}
However, when I run this function on the following way:
plot_amb2(data = tab_ambi_laps, amb_var = Chlorophyll,
time_scale = "1 hour", quant_range = c(0.15, 0.95),
xlab = "Data", ylab = "Hora", leglab = "Clorofila\n(\u00B5g/L)",
ybreak = "3 hours", xbreak = "2 month",
cor = c("lightblue", "yellow", "darkred"))
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 = "%H:%M") +
scale_x_date(date_labels = "%m-%Y", 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论