在没有明确规范的情况下使用 dplyr 在 R 中搜索字符列并将其批量转换为日期
Search and mass convert character columns to date in R with dplyr without explicite specification
我有一个包含数千个变量的混乱数据框,我想自动将特定列转换为日期,而不必明确指定哪些列。所有要转换的列的名称中都有“日期”。大多数是 mdy
,但也可以是 dmy
。有些包含错误或格式错误的日期,但比例非常小 <0.1%。
我试过了:
df %>% select(contains("Date")) %>% as_Date() #Does not work
df %>% select(contains("Date")) %>% mdy() #selecting only the columns with dates, does not work
df %>% select(contains("Date")) %>% parse_date_time( c("mdy", "dmy")) #also does not work
我想我没有得到一些基本的东西。
这是一个基于lubridate
的解决方案:
玩具数据:
df <- data.frame(Date1 = c("01-Mar-2015", "31-01-2012", "15/01/1999"),
Var_Date = c("01-02-2018", "01/08/2016", "17-09-2007"),
More_Dates = c("27/11/2009", "22-Jan-2013", "20-Nov-1987"))
# define formats:
formats <- c("%d-%m-%Y", "%d/%m/%Y", "%d-%b-%Y")
一个dyplr
解决方案:
library(dplyr)
library(lubridate)
df %>%
mutate(across(contains("Date"),
~ parse_date_time(., orders = formats))) %>%
mutate(across(contains("Date"),
~ format(., "%d-%m-%Y")))
Date1 Var_Date More_Dates
1 01-03-2015 01-02-2018 27-11-2009
2 31-01-2012 01-08-2016 22-01-2013
3 15-01-1999 17-09-2007 20-11-1987
一个base R
解决方案:
library(lubridate)
df[,grepl("Date", names(df))] <- apply(df[,grepl("Date", names(df))], 2,
function(x) format(parse_date_time(x, orders = my_formats), "%d-%m-%Y"))
我们可以使用 parsedate
中的 parse_date
library(parsedate)
library(dplyr)
df %>%
mutate(across(everything(), parse_date))
Date1 Var_Date More_Dates
1 2015-03-01 2018-01-02 2009-11-27
2 2012-01-31 2016-01-08 2013-01-22
3 1999-01-15 2007-09-17 1987-11-20
数据
df <- structure(list(Date1 = c("01-Mar-2015", "31-01-2012", "15/01/1999"
), Var_Date = c("01-02-2018", "01/08/2016", "17-09-2007"), More_Dates = c("27/11/2009",
"22-Jan-2013", "20-Nov-1987")),
class = "data.frame", row.names = c(NA,
-3L))
我有一个包含数千个变量的混乱数据框,我想自动将特定列转换为日期,而不必明确指定哪些列。所有要转换的列的名称中都有“日期”。大多数是 mdy
,但也可以是 dmy
。有些包含错误或格式错误的日期,但比例非常小 <0.1%。
我试过了:
df %>% select(contains("Date")) %>% as_Date() #Does not work
df %>% select(contains("Date")) %>% mdy() #selecting only the columns with dates, does not work
df %>% select(contains("Date")) %>% parse_date_time( c("mdy", "dmy")) #also does not work
我想我没有得到一些基本的东西。
这是一个基于lubridate
的解决方案:
玩具数据:
df <- data.frame(Date1 = c("01-Mar-2015", "31-01-2012", "15/01/1999"),
Var_Date = c("01-02-2018", "01/08/2016", "17-09-2007"),
More_Dates = c("27/11/2009", "22-Jan-2013", "20-Nov-1987"))
# define formats:
formats <- c("%d-%m-%Y", "%d/%m/%Y", "%d-%b-%Y")
一个dyplr
解决方案:
library(dplyr)
library(lubridate)
df %>%
mutate(across(contains("Date"),
~ parse_date_time(., orders = formats))) %>%
mutate(across(contains("Date"),
~ format(., "%d-%m-%Y")))
Date1 Var_Date More_Dates
1 01-03-2015 01-02-2018 27-11-2009
2 31-01-2012 01-08-2016 22-01-2013
3 15-01-1999 17-09-2007 20-11-1987
一个base R
解决方案:
library(lubridate)
df[,grepl("Date", names(df))] <- apply(df[,grepl("Date", names(df))], 2,
function(x) format(parse_date_time(x, orders = my_formats), "%d-%m-%Y"))
我们可以使用 parsedate
parse_date
library(parsedate)
library(dplyr)
df %>%
mutate(across(everything(), parse_date))
Date1 Var_Date More_Dates
1 2015-03-01 2018-01-02 2009-11-27
2 2012-01-31 2016-01-08 2013-01-22
3 1999-01-15 2007-09-17 1987-11-20
数据
df <- structure(list(Date1 = c("01-Mar-2015", "31-01-2012", "15/01/1999"
), Var_Date = c("01-02-2018", "01/08/2016", "17-09-2007"), More_Dates = c("27/11/2009",
"22-Jan-2013", "20-Nov-1987")),
class = "data.frame", row.names = c(NA,
-3L))