You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R的tidyverse中为多个datetime列动态新增星期几列的实现方法

问题:批量为数据框中所有datetime类型列提取星期几并生成新列

我有一个包含多个datetime类型列的表,希望为每一列提取对应星期几并作为新列追加到表中。

示例数据集

structure(list(mealTime = structure(c(1542492000, 1578852000, 
1604253600, 1545901200, 1549821600, 1544306400), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), weight_measure_time = structure(c(1542226000, 1578812400, 
1594710000, 1545896762, 1546416823, 1544227245), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), height_measure_time = structure(c(1542106434, 1543337043, 
1543337043, 1542387988, 1542366547, 1542802228), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), hba1c_measure_time = structure(c(1542106860, 1573455600, 
1594625400, 1544781600, 1545920520, 1544096580), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), bpMeasureTime = structure(c(1542380623, 1578812400, 
1583218800, 1545896774, 1546416837, 1544266110), tzone = "UTC", class = c("POSIXct", 
"POSIXt"))), row.names = c(NA, -6L), class = c("tbl_df", "tbl", 
"data.frame"))

数据集预览

> smple
# A tibble: 6 x 5
  mealTime            weight_measure_time height_measure_time
  <dttm>              <dttm>              <dttm>             
1 2018-11-17 22:00:00 2018-11-14 20:06:40 2018-11-13 10:53:54
2 2020-01-12 18:00:00 2020-01-12 07:00:00 2018-11-27 16:44:03
3 2020-11-01 18:00:00 2020-07-14 07:00:00 2018-11-27 16:44:03
4 2018-12-27 09:00:00 2018-12-27 07:46:02 2018-11-16 17:06:28
5 2019-02-10 18:00:00 2019-01-02 08:13:43 2018-11-16 11:09:07
6 2018-12-08 22:00:00 2018-12-08 00:00:45 2018-11-21 12:10:28
# ... with 2 more variables: hba1c_measure_time <dttm>, bpMeasureTime <dttm>

预期输出

为每个datetime列提取星期几并新增为对应后缀为_day的新列,效果如下:

glimpse(smple)
Rows: 6
Columns: 10
$ mealTime                <dttm> 2018-11-17 22:00:00, 2020-01-12 18:00:00, 20~
$ weight_measure_time     <dttm> 2018-11-14 20:06:40, 2020-01-12 07:00:00, 20~
$ height_measure_time     <dttm> 2018-11-13 10:53:54, 2018-11-27 16:44:03, 20~
$ hba1c_measure_time      <dttm> 2018-11-13 11:01:00, 2019-11-11 07:00:00, 20~
$ bpMeasureTime           <dttm> 2018-11-16 15:03:43, 2020-01-12 07:00:00, 20~
$ mealTime_day            <chr> "Saturday", "Sunday", "Sunday", "Thursday", "~
$ weight_measure_time_day <chr> "Wednesday", "Sunday", "Tuesday", "Thursday",~
$ height_measure_time_day <chr> "Tuesday", "Tuesday", "Tuesday", "Friday", "F~
$ hba1c_measure_time_day  <chr> "Tuesday", "Monday", "Monday", "Friday", "Thu~
$ bpMeasureTime_day       <chr> "Friday", "Sunday", "Tuesday", "Thursday", "W~

现有实现方案

目前可通过如下base R代码实现需求:

smple[paste(colnames(smple), "day", sep="_")] = apply(smple, 2, lubridate::wday, label=TRUE, abbr=FALSE)

希望找到tidyverse生态下的等价实现,可以同时对左右两边的表达式求值来动态添加列。


解答

tidyverse生态下可以通过dplyr的across()函数配合mutate()实现,代码更简洁易读,且原生支持动态生成列名:

library(tidyverse)
library(lubridate)

smple <- smple %>%
  mutate(across(where(is.POSIXct), 
                ~wday(.x, label = TRUE, abbr = FALSE),
                .names = "{.col}_day"))

参数说明:

  • where(is.POSIXct) 自动筛选所有datetime类型的列,无需手动指定列名,后续新增datetime列也能自动适配
  • .names 参数控制生成的新列名规则,{.col}会自动替换为原列名,直接拼接_day即可完全匹配预期的列名格式
  • 逻辑与现有base R实现完全对齐,输出结果完全一致。

如果使用的是1.0.0版本以下的旧版dplyr,可以用mutate_at实现同样效果:

smple <- smple %>%
  mutate_at(vars(where(is.POSIXct)),
            list(day = ~wday(.x, label = TRUE, abbr = FALSE)))

内容的提问来源于stack exchange,提问作者monte

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.10 11:00:02