R Class 转向 NULL 而不是日期
R Class turning to NULL instead of date
所以我有如下数据
data <- structure(list(LICENCE_RENEWAL_DATE = c("NA", "NA", "NA", "2022-02-12T07:42:49Z",
"2022-02-10T13:52:37Z", "NA", "NA", "2022-02-13T16:37:48Z", "NA",
"NA"), LICENCE_RENEWAL_DATE2 = c("NA", "NA", "NA", "2022-02-12",
"2022-02-10", "NA", "NA", "2022-02-13", "NA", "NA")), row.names = c(NA,
10L), class = "data.frame")
我通过 运行 得到这个:
data <- full_data %>%
mutate(LICENCE_RENEWAL_DATE2 = substr(LICENCE_RENEWAL_DATE, 1, 10)) %>%
select(LICENCE_RENEWAL_DATE, LICENCE_RENEWAL_DATE2)
我以为我可以将 substr()
与 lubridate
的 as_date() 夹在中间,但这不起作用。 LICENCE_RENEWAL_DATE2
的 class 为 NULL,我不知道该怎么做。但我需要它是 class = Date.
你可以这样做:
full_data$LICENCE_RENEWAL_DATE2 <- as.Date(full_data$LICENCE_RENEWAL_DATE2, format = "%Y-%m-%d")
它会将 LICENCE_RENEWAL_DATE2
列的 class 转换为 Date
。
> class(full_data$LICENCE_RENEWAL_DATE2)
[1] "Date"
您可以尝试以下解决方案**
data <- structure(
list(
LICENCE_RENEWAL_DATE = c(" ", " ", " ", "2022-02-12T07:42:49Z", "2022-02-10T13:52:37Z",
" ", " ", "2022-02-13T16:37:48Z", " ", " "),
LICENCE_RENEWAL_DATE2 = c(" ", " ", " ", "2022-02-12", "2022-02-10", " ", " ",
"2022-02-13", " ", " ")),
row.names = c(NA, 10L),class = "data.frame")
创建数据时不涉及 NA
full_data <- data %>%
mutate(LICENCE_RENEWAL_DATE2 = substr(LICENCE_RENEWAL_DATE, 1, 10)) %>%
select(LICENCE_RENEWAL_DATE, LICENCE_RENEWAL_DATE2)
转换日期时间(“POSIXct”“POSIXt”)和日期中的两列
library(anytime)
full_data$LICENCE_RENEWAL_DATE <- anytime(full_data$LICENCE_RENEWAL_DATE)
full_data$LICENCE_RENEWAL_DATE2 <- anydate(full_data$LICENCE_RENEWAL_DATE2)
检查数据类型
> class(full_data$LICENCE_RENEWAL_DATE)
[1] "POSIXct" "POSIXt"
> class(full_data$LICENCE_RENEWAL_DATE2)
[1] "Date"
我希望这个解决方案能解决您的问题
所以我有如下数据
data <- structure(list(LICENCE_RENEWAL_DATE = c("NA", "NA", "NA", "2022-02-12T07:42:49Z",
"2022-02-10T13:52:37Z", "NA", "NA", "2022-02-13T16:37:48Z", "NA",
"NA"), LICENCE_RENEWAL_DATE2 = c("NA", "NA", "NA", "2022-02-12",
"2022-02-10", "NA", "NA", "2022-02-13", "NA", "NA")), row.names = c(NA,
10L), class = "data.frame")
我通过 运行 得到这个:
data <- full_data %>%
mutate(LICENCE_RENEWAL_DATE2 = substr(LICENCE_RENEWAL_DATE, 1, 10)) %>%
select(LICENCE_RENEWAL_DATE, LICENCE_RENEWAL_DATE2)
我以为我可以将 substr()
与 lubridate
的 as_date() 夹在中间,但这不起作用。 LICENCE_RENEWAL_DATE2
的 class 为 NULL,我不知道该怎么做。但我需要它是 class = Date.
你可以这样做:
full_data$LICENCE_RENEWAL_DATE2 <- as.Date(full_data$LICENCE_RENEWAL_DATE2, format = "%Y-%m-%d")
它会将 LICENCE_RENEWAL_DATE2
列的 class 转换为 Date
。
> class(full_data$LICENCE_RENEWAL_DATE2)
[1] "Date"
您可以尝试以下解决方案**
data <- structure(
list(
LICENCE_RENEWAL_DATE = c(" ", " ", " ", "2022-02-12T07:42:49Z", "2022-02-10T13:52:37Z",
" ", " ", "2022-02-13T16:37:48Z", " ", " "),
LICENCE_RENEWAL_DATE2 = c(" ", " ", " ", "2022-02-12", "2022-02-10", " ", " ",
"2022-02-13", " ", " ")),
row.names = c(NA, 10L),class = "data.frame")
创建数据时不涉及 NA
full_data <- data %>%
mutate(LICENCE_RENEWAL_DATE2 = substr(LICENCE_RENEWAL_DATE, 1, 10)) %>%
select(LICENCE_RENEWAL_DATE, LICENCE_RENEWAL_DATE2)
转换日期时间(“POSIXct”“POSIXt”)和日期中的两列
library(anytime)
full_data$LICENCE_RENEWAL_DATE <- anytime(full_data$LICENCE_RENEWAL_DATE)
full_data$LICENCE_RENEWAL_DATE2 <- anydate(full_data$LICENCE_RENEWAL_DATE2)
检查数据类型
> class(full_data$LICENCE_RENEWAL_DATE)
[1] "POSIXct" "POSIXt"
> class(full_data$LICENCE_RENEWAL_DATE2)
[1] "Date"
我希望这个解决方案能解决您的问题