I have a nested tibble in the following form:
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))
and I would like a new column filled with p-values calculated from ks.test for each tibble in data compared to a normal distribution which parameters are stored in p1 and p2.
I tried something like
ex %>%
mutate(ks_test = map(data,
~tryCatch( #to avoid problems with tibbles filled only by NAs
ks.test(x = .$val,
y = "pnorm",
mean = .$p1,
sd = .$p2),
error = function(e) list(p.value = NA))),
ks_pvalue = map_dbl(ks_test, "p.value"))
which unfortunately fails to perform the test, providing only NAs.
Please, can you help me to fix this chunk of code? Thank you.