我没有将列名称为字符串,但是当我没有将列名称为字符串时,如何检查if语句中的类:
我的问题出在if statement
波纹管上:我尝试过rlang::as_name
,quote
等。
df <- tibble::tibble( time_text = as.character(as.POSIXct("2018-02-03 08:00:00", tz = "UTC") + rnorm(100, 0, 60*60*60)))
date_from_text <- function(df, x){
if(!class(df[[deparse(x)]]) %in% c("POSIXct", "POSIXt" )) {
x <- rlang::enquo(x)
name <- rlang::quo_name(x)
out <- df %>%
dplyr::mutate(!!name := lubridate::ymd_hms(!!x))
}
else {
stop("Seems that column is in the right format already")
}
}
date_from_text(df, time_text)
Error in deparse(x) : object 'time_text' not found
答案 0 :(得分:3)
当您在x <- rlang::enquo(x)
声明之前使用name <- rlang::quo_name(x)
和if
时,它会起作用:
date_from_text <- function(df, x){
x <- rlang::enquo(x)
name <- rlang::quo_name(x)
if(!inherits(df[[name]], c("POSIXct", "POSIXt"))) {
out <- dplyr::mutate(df, !!name := lubridate::ymd_hms(!!x))
} else {
stop("Seems that column is in the right format already")
}
}
我将if
语句中的要求更改为!inherits(df[[name]], c("POSIXct", "POSIXt"))
。
在您的原始代码中,仅会检查类向量的第一个元素,而继承会检查是否继承了任何指定的类。
my.df <- tibble::tibble(time_text = as.character(as.POSIXct("2018-02-03 08:00:00", tz = "UTC") + rnorm(100, 0, 60*60*60)))
my.df2 <- date_from_text(my.df, time_text)
my.df2
# A tibble: 100 x 1
# time_text
# <dttm>
# 1 2018-02-06 18:38:46
# 2 2018-01-31 16:16:15
# 3 2018-02-04 05:52:32
# 4 2018-02-05 23:31:50
# 5 2018-02-06 13:00:34
# 6 2018-02-01 16:16:24
# 7 2018-02-05 15:09:45
# 8 2018-02-04 04:23:00
# 9 2018-02-03 06:55:18
# 10 2018-01-29 01:06:26
# ... with 90 more rows
date_from_text(my.df2, time_text)
date_from_text(my.df2,time_text)中的错误: 似乎该列已经采用了正确的格式
感谢@KonradRudolph通过他的评论改进了这个答案。
答案 1 :(得分:2)
我们还可以使用{{}}
中新的curl-curly(rlang
)运算符
library(rlang)
date_from_text <- function(df, x){
if (!class(df %>% pull({{x}})) %in% c("POSIXct", "POSIXt")) {
x <- rlang::enquo(x)
name <- rlang::quo_name(x)
df %>% mutate(!!name := lubridate::ymd_hms({{x}}))
} else {
stop("Seems that column is in the right format already")
}
}
df1 <- date_from_text(df, time_text)
df1
# A tibble: 100 x 1
# time_text
# <dttm>
# 1 2018-02-05 06:47:00.947
# 2 2018-02-06 13:25:36.656
# 3 2018-01-31 18:45:57.358
# 4 2018-02-05 02:16:56.179
# 5 2018-02-06 15:43:30.417
# 6 2018-02-07 14:59:56.203
# 7 2018-02-01 04:25:29.382
# 8 2018-01-31 19:11:13.136
# 9 2018-02-02 18:47:06.812
#10 2018-02-03 17:31:45.790
# … with 90 more rows
date_from_text(df1, time_text)
date_from_text(df1,time_text)中的错误: 似乎该列已经采用了正确的格式
答案 2 :(得分:1)
或引用要检查的列名
date_from_text <- function(df, x){
if( !class( df[[x]] ) %in% c( "POSIXct", "POSIXt" )) {
df[[x]] <- lubridate::ymd_hms( df[[x]] )
return( df[[x]] )
}
else {
stop( "Seems that column is in the right format already" )
}
}
date_from_text( df, "time_text" )