mce1
mce1

Reputation: 25

passing values from multiple columns into dplyr summarise function

please consider the following minimal example: I have combined observations from two experiments A and B as a dplyr tibble. llim and ulim define the lower and upper limits of the observable in each group.

library(dplyr)

name <- factor  (c  (rep('A', 400), 
                    rep('B', 260)
                    )
                )

obs <- c    (sample(-23:28, 400, replace = TRUE),
            sample(-15:39, 260, replace = TRUE)
            )

llim <- c   (rep(-23, 400), 
            rep(-15, 260)
            )

ulim <- c   (rep(28, 400), 
            rep(39, 260)
            )

tib1 <- tibble  (name, obs, llim, ulim)

tib1
# A tibble: 660 x 4
   name    obs  llim  ulim
   <fct> <int> <dbl> <dbl>
 1 A        22   -23    28
 2 A        -5   -23    28
 3 A         2   -23    28
 4 A         9   -23    28
 5 A        -1   -23    28
 6 A       -21   -23    28
 7 A        13   -23    28
 8 A         0   -23    28
 9 A         8   -23    28
10 A       -11   -23    28
# … with 650 more rows

Next, I compute a histogram of the observable per each group. This works fine as long as I use the default parameters of hist().

tib1 %>%    group_by(name) %>%

            summarise   (counts = hist(obs, plot = FALSE)$counts)

`summarise()` has grouped output by 'name'. You can override using the `.groups` argument.
# A tibble: 22 x 2
# Groups:   name [2]
   name  counts
   <fct>  <int>
 1 A         26
 2 A         44
 3 A         39
 4 A         32
 5 A         42
 6 A         34
 7 A         44
 8 A         41
 9 A         39
10 A         37
# … with 12 more rows

Now, I would like to adjust these histograms using further group-specific parameters stored within the tibble, e.g. llim and ulim. This, however, does not seem to work:

tib1 %>%    group_by(name) %>%

            summarise   (counts = hist  (obs, 
                                        breaks = seq    (llim,
                                                        ulim,
                                                        by = 1
                                                        ),
                                        plot = FALSE
                                        )$counts
                        )
Error: Problem with `summarise()` input `counts`.
✖ 'from' must be of length 1
ℹ Input `counts` is `hist(obs, breaks = seq(llim, ulim, by = 1), plot = FALSE)$counts`.
ℹ The error occurred in group 1: name = "A".
Run `rlang::last_error()` to see where the error occurred.
                    
                    

Is there a way to pass values from columns llim and ulim to the hist() function? Or is there a different problem? The error message is a bit cryptic...

Your help would be much appreciated!

Upvotes: 0

Views: 54

Answers (2)

mce1
mce1

Reputation: 25

Reducing the length of llim and ulim to 1 (using e.g. max() or min()) does the trick:

tib1 %>%    group_by(name, llim, ulim) %>%

            summarise   (counts = hist  (obs, 
                                        breaks = seq    (max(llim),
                                                        max(ulim),
                                                        by = 1
                                                        ),
                                        plot = FALSE
                                        )$counts
                        )
# A tibble: 105 x 4
# Groups:   name, llim, ulim [2]
   name   llim  ulim counts
   <fct> <dbl> <dbl>  <int>
 1 A       -23    28      9
 2 A       -23    28      9
 3 A       -23    28      8
 4 A       -23    28      7
 5 A       -23    28      5
 6 A       -23    28      8
 7 A       -23    28     14
 8 A       -23    28     10
 9 A       -23    28      9
10 A       -23    28      9
# … with 95 more rows

So the error message made sense in the end...

Upvotes: 0

TarJae
TarJae

Reputation: 78917

This gives a histogram of obs by group name

library(ggplot2)
ggplot(tib1, aes(x = obs)) +
  geom_histogram(aes(color = name, fill = name),
                 position = "identity", bins = 30, alpha = 0.4) +
  scale_color_manual(values = c("blue", "red")) +
  scale_fill_manual(values = c("blue", "red"))

enter image description here

Upvotes: 1

Related Questions