如何将多个值从dplyr summarise_all结构内的1个函数导出到结果表中的列

时间:2019-05-13 11:53:19

标签: r dplyr

我试图弄清楚如何导出在summarise_all内使用的函数中计算出的1个值而不是2个值

library(tidyr)
library(dplyr)
library(data.table)
library(diptest)

funx <- function(x) {
  logtest <- suppressWarnings(log10(x))
  remaining <- length(logtest[which(!is.na(logtest) & is.finite(logtest))])
  x <- if(remaining > 0.75*length(x)) {suppressWarnings(log10(x)) } else { x }
  logchoice <- if(remaining > 0.75*length(x)) {'Y' } else { 'N' }
  x <- x[which(!is.na(x) & is.finite(x))]
  y <- diptest::dip.test(x[1:7200])
  z <- y$p.value

  # z <- list(pvalue = y$p.value, transform = logchoice) ## attempt at exporting 2 values from the function
  return(z)
  }

到目前为止,我通过实验了解到了这一点:

mtcars %>% sample_n(30) %>%
select(colnames(mtcars)[2:5]) %>%
summarise_all(list(~ list(funx(.)))) %>% gather %>% separate(value, c('pvalue', 'trans'), ',')

这给了我这个:

   key                             pvalue             trans
1  cyl list(pvalue = 6.20997213685026e-06  transform = "T")
2 disp     list(pvalue = 0.14200504058625  transform = "T")
3   hp   list(pvalue = 0.0549071023871164  transform = "T")
4 drat   list(pvalue = 0.0138972262215915  transform = "T")

或者如果我不命名列表元素,则使用以下方法:

   key                   pvalue trans
1  cyl                   list(0  "T")
2 disp   list(0.112335716284263  "T")
3   hp  list(0.0425200071960111  "T")
4 drat list(0.00752301601012173  "T")

所以我有一种错误的感觉。

1 个答案:

答案 0 :(得分:0)

mtcars %>% 
sample_n(30) %>%
select(colnames(mtcars)[2:5]) %>%
summarise_all(list(~ list(funx(.)))) %>% 
gather %>% 
unnest 

给我我需要的东西:

  Parameter     pvalue Transform
1       cyl 0.00000000         T
2      disp 0.03095694         T
3        hp 0.08493466         T
4      drat 0.11180833         T