将字符列转换为具有多种格式的日期
Convert a a character column to date with multiple formats
这是我从 excel 开始的示例数据(下一次,我将教我的客户整理数据):
date_string
3/13, 3/17, 3/20
4/13
5/12, 5/20
我已经非常接近我想要的了:
library(tidyverse)
library(stringr)
data <- str_split_fixed(data$date_string, ",", 3)%>%
as_tibble() %>%
gather() %>%
filter(value != "")
然后我剩下这个:
key value
v1 3/13
v1 43203
v1 5/12
v2 3/17
v2 5/20
v3 3/20
这已经足够了,我可以在 excel 中完成其余的格式化和排列,但我在 R 中能做的越多越好,尤其是因为接下来我将不得不再次完成这一切是时候更新最终产品了。我觉得有一个 lubridate
函数可以帮助我解决这个问题,但是 mdy
和 date
不断返回错误。
我要的值是上面的table,但是格式是m/d/y。
更新
根据下面的答案,我添加了以下内容。这可行,但可能有更优雅的方法:
data <- str_split_fixed(data$date_string, ",", 3)%>%
as_tibble() %>%
gather() %>%
filter(value != "") %>%
mutate(value =
if_else(
str_detect(value, "/") == T,
paste0(value, "/2018"),
as.character(as_date(as.numeric(value), origin = "1900-01-01")))) %>%
mutate(value =
if_else(
str_detect(value, "/") == T,
mdy(value),
ymd(value)))
我收到这些警告,但数据是我想要的:
1. In as_date(as.numeric(value), origin = "1900-01-01") :
NAs introduced by coercion
2. 1 failed to parse.
3. 5 failed to parse.
不确定如何 "failed to parse" 当最后的 "value" 列作为日期变量返回时。 . .
您可以执行以下操作:
首先,将年份添加到您的日期中:
dates <- ("3/13", "4/17", "5/12", "3/17", "5/20", "3/20")
dates <- paste0(dates, "/18")
其次,转换它们指定格式(m/d/y 在你的情况下):
as.Date(dates, "%m/%d/%y")
[1] "2018-03-13" "2018-04-17" "2018-05-12" "2018-03-17" "2018-05-20" "2018-03-20"
这里有一个稍微不同的方法:
library(tidyverse)
library(stringr)
library(lubridate)
data <- c(
"3/13, 3/17, 3/20",
"4/13",
"5/12, 5/20")
df <- tibble(date_string = data) %>%
mutate(date_string = str_split(date_string, ", ")) %>%
unnest() %>%
mutate(date_string = ymd(str_c("2018-", date_string)))
df
这是我从 excel 开始的示例数据(下一次,我将教我的客户整理数据):
date_string
3/13, 3/17, 3/20
4/13
5/12, 5/20
我已经非常接近我想要的了:
library(tidyverse)
library(stringr)
data <- str_split_fixed(data$date_string, ",", 3)%>%
as_tibble() %>%
gather() %>%
filter(value != "")
然后我剩下这个:
key value
v1 3/13
v1 43203
v1 5/12
v2 3/17
v2 5/20
v3 3/20
这已经足够了,我可以在 excel 中完成其余的格式化和排列,但我在 R 中能做的越多越好,尤其是因为接下来我将不得不再次完成这一切是时候更新最终产品了。我觉得有一个 lubridate
函数可以帮助我解决这个问题,但是 mdy
和 date
不断返回错误。
我要的值是上面的table,但是格式是m/d/y。
更新
根据下面的答案,我添加了以下内容。这可行,但可能有更优雅的方法:
data <- str_split_fixed(data$date_string, ",", 3)%>%
as_tibble() %>%
gather() %>%
filter(value != "") %>%
mutate(value =
if_else(
str_detect(value, "/") == T,
paste0(value, "/2018"),
as.character(as_date(as.numeric(value), origin = "1900-01-01")))) %>%
mutate(value =
if_else(
str_detect(value, "/") == T,
mdy(value),
ymd(value)))
我收到这些警告,但数据是我想要的:
1. In as_date(as.numeric(value), origin = "1900-01-01") :
NAs introduced by coercion
2. 1 failed to parse.
3. 5 failed to parse.
不确定如何 "failed to parse" 当最后的 "value" 列作为日期变量返回时。 . .
您可以执行以下操作:
首先,将年份添加到您的日期中:
dates <- ("3/13", "4/17", "5/12", "3/17", "5/20", "3/20")
dates <- paste0(dates, "/18")
其次,转换它们指定格式(m/d/y 在你的情况下):
as.Date(dates, "%m/%d/%y")
[1] "2018-03-13" "2018-04-17" "2018-05-12" "2018-03-17" "2018-05-20" "2018-03-20"
这里有一个稍微不同的方法:
library(tidyverse)
library(stringr)
library(lubridate)
data <- c(
"3/13, 3/17, 3/20",
"4/13",
"5/12, 5/20")
df <- tibble(date_string = data) %>%
mutate(date_string = str_split(date_string, ", ")) %>%
unnest() %>%
mutate(date_string = ymd(str_c("2018-", date_string)))
df