使用函数mutate_at不能按预期迭代该函数

时间:2019-05-07 14:08:29

标签: r dplyr mutate

我有一列希望将其转换为秒。转换函数有效,但是当我尝试使用mutate_at来迭代多个列时。它没有按我预期的那样工作。我不知道我在mutate_at语法中缺少什么。

我有这个:

catalog
# A tibble: 4 x 3
#  file                              start end  
#  <chr>                             <chr> <chr>
#1 20190506_205959-20190506_210459   1:58  3:00 
#2 20190506_210507-20190506_211007   0     0:32 
#3 20190506_205959-20190506_210459_2 0     3:18 
#4 20190506_220712-20190506_221210   0     5  

transform_time_to_seconds <- function(x) {
    x %>% 
        str_split(":", simplify = TRUE) %>% 
        as.numeric() %>% 
        {.[1] * 60 + 
         ifelse(is.na(.[2]), 0, .[2])}
}

我应用了mutate_at

catalog %>%
    mutate_at(vars(start, end), transform_time_to_seconds)
# A tibble: 4 x 3
#  file                              start   end
#  <chr>                             <dbl> <dbl>
#1 20190506_205959-20190506_210459      60   180
#2 20190506_210507-20190506_211007      60   180
#3 20190506_205959-20190506_210459_2    60   180
#4 20190506_220712-20190506_221210      60   180

但是我期望的是:

catalog %>%
    mutate(start = map_dbl(start, transform_time_to_seconds),
           end   = map_dbl(end, transform_time_to_seconds))
# A tibble: 4 x 3
#  file                              start   end
#  <chr>                             <dbl> <dbl>
#1 20190506_205959-20190506_210459     118   180
#2 20190506_210507-20190506_211007       0    32
#3 20190506_205959-20190506_210459_2     0   198
#4 20190506_220712-20190506_221210       0   300

有什么建议吗?


catalog数据:

structure(list(file = c("20190506_205959-20190506_210459", "20190506_210507-20190506_211007", 
"20190506_205959-20190506_210459_2", "20190506_220712-20190506_221210"
), start = c("1:58", "0", "0", "0"), end = c("3:00", "0:32", 
"3:18", "5")), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -4L), spec = structure(list(cols = list(
    file = structure(list(), class = c("collector_character", 
    "collector")), start = structure(list(), class = c("collector_character", 
    "collector")), end = structure(list(), class = c("collector_character", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), skip = 1), class = "col_spec"))
`` 

2 个答案:

答案 0 :(得分:5)

您还可以vectorize使用功能

transform_time_to_seconds <- Vectorize(transform_time_to_seconds)

答案 1 :(得分:2)

在传递整列时,您的函数一次期望一个值。

添加rowwise可能会有帮助

library(dplyr)

catalog %>%
  rowwise() %>%
  mutate_at(vars(start, end), transform_time_to_seconds)

# A tibble: 4 x 3
#  file                              start   end
#  <chr>                             <dbl> <dbl>
#1 20190506_205959-20190506_210459     118   180
#2 20190506_210507-20190506_211007       0    32
#3 20190506_205959-20190506_210459_2     0   198
#4 20190506_220712-20190506_221210       0   300