在嵌套的小标题上变异

时间:2019-01-04 13:42:13

标签: r dplyr purrr mutate tibble

我有一个嵌套的tibble,格式如下:

library(purrr)
library(dplyr)

ex <- tibble(data = list(val = rnorm(12), 
                         val = rnorm(5), 
                         val = rep(NA, 5), 
                         val = c(rnorm(3), NA)), 
               p1 = rnorm(4),
               p2 = rnorm(4)) %>% 
         mutate(data = map(data, tibble))

,我想要一个新列,其中填充了 p 值,该值是根据ks.test中每个tibble的{​​{1}}计算出的,与正态分布相比存储在datap1中。

我尝试过类似的

p2

很遗憾,该测试未能执行测试,仅提供了NA。

请,您能帮我修复这段代码吗? 谢谢。

1 个答案:

答案 0 :(得分:2)

我将转介您purrr::possibly来处理您的不适用案件。至于处理完整的向量,最好有一个分组列作为嵌套依据。 ks_test的特定功能还有助于保持环境清洁。

library(purrr)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(tidyverse)


val1 = data.frame(val = rnorm(12), group = 1)
val2 = data.frame(val = rnorm(5), group = 2)

ks_test_fun <- function(data, p1, p2, ...){
  data %>% 
  ks.test(x = .$val, y = "pnorm", mean = p1, sd = p2) %>% 
  broom::tidy() %>% 
  pull(p.value)
}

set.seed(4)
rbind(val1, val2) %>% 
  group_by(group) %>% 
  nest() %>% 
  mutate(p1 = rnorm(2), p2 = rnorm(2)) %>% 
  mutate(ks_test_pval = map(data, ks_test_fun, p1, p2)) %>% 
  unnest(ks_test_pval)
#> # A tibble: 2 x 5
#>   group data                  p1    p2 ks_test_pval
#>   <dbl> <list>             <dbl> <dbl>        <dbl>
#> 1     1 <tibble [12 x 1]>  0.217 0.891        0.137
#> 2     2 <tibble [5 x 1]>  -0.542 0.596        0.333

reprex package(v0.2.1)于2019-01-04创建