我的資料框中有很多日期列,需要將所有日期轉換為每個日期的三個新列(月份、月份和年份)
我盡量避免專門寫下每一列,所以我正在使用across但仍然圍繞這種動詞
library(tidyverse)
df <- tibble(dates1 = c("2020-08-03", "2020-08-03"),
dates2 = c("2020-08-05", "2020-08-05"))
df %>% mutate(across(contains("dates"), lubridate::day(.), lubridate::month(.), lubridate::year(.) ))
Error: Problem with `mutate()` input `..1`.
i `..1 = across(...)`.
x do not know how to convert 'x' to class “POSIXlt”
并猜測新列應該如何命名....我有點迷茫
uj5u.com熱心網友回復:
嘗試在一個 list
library(dplyr)
df %>%
mutate(across(contains("dates"), list(day = ~ lubridate::day(.),
month = ~ lubridate::month(.), year = ~lubridate::year(.) )))
-輸出
# A tibble: 2 × 8
dates1 dates2 dates1_day dates1_month dates1_year dates2_day dates2_month dates2_year
<chr> <chr> <int> <dbl> <dbl> <int> <dbl> <dbl>
1 2020-08-03 2020-08-05 3 8 2020 5 8 2020
2 2020-08-03 2020-08-05 3 8 2020 5 8 2020
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/358128.html
