目前,我已经使用 fread 将数据导入到 R 中,并且导入工作正常。然而,对于 2 列,日期被读取为“d.m.Y”并保存为字符。问题是这些列应该采用日期格式并采用以下格式“Y-m-d”。我只是无法相应地调整这些列...
# Function for standardizing the date format
standardize_date_format <- function(df) {
date_columns <- c("Column1", "Column2")
for (col in date_columns) {
if (col %in% names(df)) {
df[[col]] <- parse_date_time(df[[col]], orders = c("dmy", "mdy", "ymd"))
}
}
return(df)
}
# Standardize the date format in the data frame
data.frame <- standardize_date_format(old)
试试这个:
# Toy data
my_df <- structure(
list(
some_id = c(4785L, 1502L, 4429L, 4695L, 5089L, 8885L, 4051L, 9191L),
col_date_1 = c(
"16.04.2024", "17.04.2024", "18.04.2024", "19.04.2024",
"20.04.2024", "21.04.2024", "22.04.2024", "23.04.2024"),
col_date_2 = c(
"16.04.2024", "17.04.2024", "18.04.2024", "19.04.2024",
"20.04.2024", "21.04.2024", "22.04.2024", "23.04.2024")),
class = "data.frame", row.names = c(NA, -8L))
# Columns to parse
my_cols <- c("col_date_1", "col_date_2")
# Parsing dates with lubridate::dmy()
library(dplyr)
library(lubridate)
# ----------------
my_df <- mutate(my_df, across(any_of(my_cols), \(x) lubridate::dmy(x)))
输出:
> my_df
some_id col_date_1 col_date_2
1 4785 2024-04-16 2024-04-16
2 1502 2024-04-17 2024-04-17
3 4429 2024-04-18 2024-04-18
4 4695 2024-04-19 2024-04-19
5 5089 2024-04-20 2024-04-20
6 8885 2024-04-21 2024-04-21
7 4051 2024-04-22 2024-04-22
8 9191 2024-04-23 2024-04-23
> str(my_df)
'data.frame': 8 obs. of 3 variables:
$ some_id : int 4785 1502 4429 4695 5089 8885 4051 9191
$ col_date_1: Date, format: "2024-04-16" "2024-04-17" "2024-04-18" "2024-04-19" ...
$ col_date_2: Date, format: "2024-04-16" "2024-04-17" "2024-04-18" "2024-04-19" ...