I'd like to have KDE probability on a data frame grouped by multiple columns. Tried:
d <- data %>%
group_by(type,culture,set) %>%
do(density(.$obs))
Error in .density(.$obs) : could not find function ".density"
I'd like to have KDE probability on a data frame grouped by multiple columns. Tried:
d <- data %>%
group_by(type,culture,set) %>%
do(density(.$obs))
Error in .density(.$obs) : could not find function ".density"
You should always provide minimal reproducible sample data along with your post, so that SO respondents have something to work with. Sample data also often helps us to understand what it is you're trying to do and avoids ambiguities regarding data types.
That aside, a more canonical tidyverse
approach would be to nest
data and then map
density
to the relevant column. Following is an example based on mtcars
library(tidyverse)
res <- mtcars %>%
group_by(gear) %>%
nest() %>%
mutate(dens = map(data, ~density(.x$mpg)))
res
## A tibble: 3 x 3
# gear data dens
# <dbl> <list> <list>
#1 4 <tibble [12 × 10]> <S3: density>
#2 3 <tibble [15 × 10]> <S3: density>
#3 5 <tibble [5 × 10]> <S3: density>
Note how res$dens
is a list
of density
objects.