作为对此问题的关注: extract nth value in nested list with map_dbl
我很难理解为什么以下代码在tbl中只有一行而不是多行时有效。
x_n %>%
mutate( vald_end = data %>% map_dbl( ., function(x) max(x$time_cum)),
h1 = vald_end - 15*60,
index_1 = data %>% map_dbl( ., function(x) which.max(x$time_cum > h1)),
index_2 = index_1 - 1,
vald_start = data %>% map_dbl( ., ~ nth( .$time_cum,
n = index_1,
order_by = .$time_cum) )) ->
x_r
如果x_n等于
> x
# A tibble: 113 x 6
meta_event meta_aktivitet meta_subject_id time time_cum vo2
<chr> <chr> <chr> <chr> <dbl> <dbl>
1 001 001 100001 0:10 min 10 1.66
2 001 001 100001 0:15 min 15 2.52
3 001 001 100001 0:20 min 20 2.64
4 001 001 100001 0:25 min 25 2.68
5 001 001 100001 0:30 min 30 2.66
6 001 001 100001 0:35 min 35 2.71
7 001 001 100001 0:40 min 40 2.95
8 001 001 100001 0:45 min 45 2.09
9 001 001 100001 0:50 min 50 2.58
10 001 001 100001 0:55 min 55 2.91
# ... with 103 more rows
>
> x %>%
+ group_by( .,
+ meta_event,
+ meta_aktivitet,
+ meta_subject_id) %>%
+ filter( ., meta_subject_id == "100001") %>%
+ nest() ->
+ x_n
> x_n
# A tibble: 3 x 4
meta_event meta_aktivitet meta_subject_id data
<chr> <chr> <chr> <list>
1 001 001 100001 <tibble [36 x 3]>
2 002 001 100001 <tibble [39 x 3]>
3 003 001 100001 <tibble [38 x 3]>
>
我在运行代码时R抛出以下错误
x_n %>%
mutate( vald_end = data %>% map_dbl( ., function(x) max(x$time_cum)),
h1 = vald_end - 15*60,
index_1 = data %>% map_dbl( ., function(x) which.max(x$time_cum > h1)),
index_2 = index_1 - 1,
vald_start = data %>% map_dbl( ., ~ nth( .$time_cum,
n = index_1,
order_by = .$time_cum) )) ->
x_r
x_r
Error in mutate_impl(.data, dots) :
Evaluation error: length(n) == 1 is not TRUE.
In addition: Warning message:
In x$time_cum > h1 :
Hide Traceback
Rerun with Debug
Error in mutate_impl(.data, dots) :
Evaluation error: length(n) == 1 is not TRUE.
12. stop(structure(list(message = "Evaluation error: length(n) == 1 is not TRUE.",
call = mutate_impl(.data, dots), cppstack = NULL), class = c("Rcpp::eval_error",
"C++Error", "error", "condition")))
11. mutate_impl(.data, dots)
10. mutate.tbl_df(., vald_end = data %>% map_dbl(., function(x) max(x$time_cum)),
h1 = vald_end - 15 * 60, index_1 = data %>% map_dbl(., function(x) which.max(x$time_cum >
h1)), index_2 = index_1 - 1, vald_start = data %>% map_dbl(.,
~nth(.$time_cum, n = index_1, order_by = .$time_cum)))
9. mutate(., vald_end = data %>% map_dbl(., function(x) max(x$time_cum)),
h1 = vald_end - 15 * 60, index_1 = data %>% map_dbl(., function(x) which.max(x$time_cum >
h1)), index_2 = index_1 - 1, vald_start = data %>% map_dbl(.,
~nth(.$time_cum, n = index_1, order_by = .$time_cum)))
8. function_list[[k]](value)
7. withVisible(function_list[[k]](value))
6. freduce(value, `_function_list`)
5. `_fseq`(`_lhs`)
4. eval(quote(`_fseq`(`_lhs`)), env, env)
3. eval(quote(`_fseq`(`_lhs`)), env, env)
2. withVisible(eval(quote(`_fseq`(`_lhs`)), env, env))
1. x_n %>% mutate(vald_end = data %>% map_dbl(., function(x) max(x$time_cum)),
h1 = vald_end - 15 * 60, index_1 = data %>% map_dbl(., function(x) which.max(x$time_cum >
h1)), index_2 = index_1 - 1, vald_start = data %>% map_dbl(.,
~nth(.$time_cum, n = index_1, order_by = .$time_cum)))
>
但是如果我过滤x使得x_n只有一行,我得到以下结果
> x %>%
+ group_by( .,
+ meta_event,
+ meta_aktivitet,
+ meta_subject_id) %>%
+ filter( ., meta_event == "001") %>%
+ nest() ->
+ x_n
> x_n
# A tibble: 1 x 4
meta_event meta_aktivitet meta_subject_id data
<chr> <chr> <chr> <list>
1 001 001 100001 <tibble [36 x 3]>
> x_n %>%
+ mutate( vald_end = data %>% map_dbl( ., function(x) max(x$time_cum)),
+ h1 = vald_end - 15*60,
+ index_1 = data %>% map_dbl( ., function(x) which.max(x$time_cum > h1)),
+ index_2 = index_1 - 1,
+ vald_start = data %>% map_dbl( ., ~ nth( .$time_cum,
+ n = index_1,
+ order_by = .$time_cum) )) ->
+ x_r
> x_r
# A tibble: 1 x 9
meta_event meta_aktivitet meta_subject_id data vald_end h1 index_1 index_2 vald_start
<chr> <chr> <chr> <li> <dbl> <dbl> <dbl> <dbl> <dbl>
1 001 001 100001 <ti~ 195 -705 1 0 10
>
似乎nth()中的n = index_1正在提取整列。有办法避免这种情况吗?
来自丹麦的问候 丹·奥莱森