0

I am trying to perform a few simple arithmetic commands via an equally simple database I have create. Here is my database:


advertisement <- c("A", "B", "C")
times_shown <- c(900, 1000, 800)
clicks <- c(140, 180, 105)
leads <- c(55, 54, 40)


advertisement_all <- data.frame(advertisement, times_shown, clicks, leads)

advertisement_all(head)

advertisement times_shown clicks leads
1             A         900    140    55
2             B        1000    180    54
3             C         800    105    40

I would now like to perform the following three calculations: (1) clicks / times_shown (2) leads / clicks (3) leads / times_shown

I would also like them grouped by advertisement (i.e., "A", "B", "C")

This is what I am (incorrectly) entering to perform these calculations:

advertisement_all %>%
  clicks /times_shown %>%
  leads / clicks %>%
  leads / times_shown %>%
  group_by(advertisement)

And this is the error message I receive:

advertisement_all %>%
+   clicks /times_shown %>%
+   leads / clicks %>%
+   leads / times_shown %>%
+   group_by(advertisement)
Error in clicks(.) : could not find function "clicks"

Your help is most appreciated. Please note that I am new to R, so any assistance which is spelled out is not taken as an insult. ; )

  • `advertisement_all %>% group_by(advertisement) %>% summarise( clicks /times_shown , leads / clicks , leads / times_shown )` – maydin Jul 26 '21 at 15:47
  • 1
    Many thanks, Maydin! Have a nice day. = ) – Benjamin Newton Jul 26 '21 at 15:51
  • Does this answer your question? [How to sum a variable by group](https://stackoverflow.com/questions/1660124/how-to-sum-a-variable-by-group) – maydin Jul 26 '21 at 15:52
  • Pipes are intended to pass the output of one function to another. Your code doesn't do that until you call `group_by` (presumably `dplyr::group_by`). Those rate calculations should be inside of a function like `dplyr::mutate` or `dplyr::summarise`, not just on their own – camille Jul 27 '21 at 04:34

0 Answers0