R: Tidy a kernel density estimate object from the ks package

kde_tidiers {broom} | R Documentation

## Tidy a kernel density estimate object from the ks package

### Description

Tidy a kernel density estimate object, into a table with one row for each point in the estimated grid, and one column for each dimension (along with an `estimate` column with the estimated density).

### Usage

    ## S3 method for class 'kde'
    tidy(x, ...)

### Arguments

`x` | A "ks" object from the kde package
`...` | Extra arguments, not used

### Value

A data frame with one row for each point in the estimated grid. The result contains one column (named `x1`, `x2`, etc) for each dimension, and an `estimate` column containing the estimated density.

### Examples

    if (require("ks", quietly = TRUE)) {
      dat <- replicate(2, rnorm(100))
      k <- kde(dat)

      td <- tidy(k)
      head(td)

      library(ggplot2)
      ggplot(td, aes(x1, x2, fill = estimate)) +
        geom_tile() +
        theme_void()

      # also works with 3 dimensions
      dat3 <- replicate(3, rnorm(100))
      k3 <- kde(dat3)

      td3 <- tidy(k3)
      head(td3)
    }

[Package broom version 0.4.4 Index]