我在 R 中每隔 15 分钟记录一次天气数据,我需要对数据进行格式化,使每个
day's
数据后面跟着 two blank rows
,并在 date
上写上相应的 top
。以下是所需输出的一瞥:
我有一个数据框(df),其中包含日期、温度、降水量和风速列,如下所示。如何在 R 中实现这种格式设置?
这是可重现的示例:
df <- structure(list(date = structure(c(1401104700, 1401105600, 1401106500,
1401107400, 1401108300, 1401148800, 1401149700, 1401150600, 1401151500,
1401152400, 1401235200, 1401236100, 1401237000, 1401237900, 1401238800
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), temperature = c(25,
25.2, 25.3, 25.1, 25.4, 18.6, 18.3, 18.2, 18.2, 18.2, 19.7, 19.1,
18.7, 18.5, 18.3), precipitation = c(0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0), wind_speed = c(1.006, 1.006, 0.9054, 0.6036,
0.4024, 0.1006, 0.2012, 0.503, 0.1006, 0, 0, 0.1006, 0.2012,
0.1006, 0.2012)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-15L))
我首先创建一个函数,可以从数据框中获取一天的数据,并在该块的开头添加所需的行。它还会将日期转换为您在问题中显示的格式。请注意,所有列都必须转换为字符,因为标题是字符。
add_header <- function(df) {
day <- format(df$date[1], "%d_%m_%y")
header <- tibble(
date = c("", "", day, "Time"),
temperature = c("", "", "", "Temp. (f)"),
precipitation = c("", "", "", "Precipitation"),
wind_speed = c("", "", "", "Wind Speed"))
df <- df %>%
mutate(date = format(date, format = "%Y%m%d%H%M PST")) %>%
mutate(across(everything(), as.character))
bind_rows(header, df)
}
接下来,我按天对数据框进行分组,并将该函数应用于每个组。我使用
reframe()
因为我想每组返回多行。
df_new <- df %>%
group_by(day = floor_date(date, "day")) %>%
reframe(add_header(pick(everything()))) %>%
select(-day)
写入 csv 文件时,请确保省略标题:
write_csv(df_new, "data.xlsx", col_names = FALSE)