1

I am trying to be 'smart' and pipe into a summary function (in this case: quantile) with an output length > 1, but without using summarise_at etc.

Dummy data:

set.seed(1)
id = 1:20
value = sample (20:100, size = 20)
my_dat = data.frame( id, value)

So. My approach was the following:

require(dplyr)
#This data and operations are obviously simplified and do not reflect the entire complexity of my real data, but melt it down to the core of the problem)

my_dat %>% quantile(.$value, probs = seq(0, 1, 0.25), na.rm = TRUE)
#interestingly, I have to specify both the probs argument (why??) 
#and add na.rm = TRUE in order to avoid a warning, although there are no NA's... ???

which gives a result, but not the correct one, because it basically takes both columns as argument.

 0%   25%   50%   75%  100% 
 1.00 10.75 20.50 50.25 98.00

What is wrong with my code?

How to do this type of piping? I am stuck at how to basically isolate one column as vector and call a function on it.
If there is already a similar question out there many apologies for the dupe and very grateful if you could point me towards it.

tjebo
  • 21,977
  • 7
  • 58
  • 94

2 Answers2

2

quantile function in r just takes a vector as the first argument and not a dataframe. so you can do something like:

my_dat$value%>%quantile(seq(0, 1, 0.25))
   0%   25%   50%   75%  100% 
24.00 42.50 65.50 84.25 98.00

if you truly need to use the pipe to the whole dataframe, then use the curly brackets:

 my_dat%>%{quantile(.$value,seq(0, 1, 0.25))}
   0%   25%   50%   75%  100% 
24.00 42.50 65.50 84.25 98.00 
Onyambu
  • 67,392
  • 3
  • 24
  • 53
  • Thanks, but that's working for this simplified example. My piping operations contain several filter/select operations. – tjebo Apr 08 '18 at 17:00
  • This here solves the question asked. Maybe you would need to do a more elaboration on what you need – Onyambu Apr 08 '18 at 17:03
  • I thought it would be obvious that I had simplified my operations, but you're right, this needs to be specified. Apologies. Interesting with the curley brackets! – tjebo Apr 08 '18 at 17:07
2

There are a couple of pipe operators. For example the exposition-pipe operator %$% (package magrittr):

Expose the names in lhs to the rhs expression. This is useful when functions do not have a built-in data argument.

my_dat %$% quantile(value)

   0%   25%   50%   75%  100% 
24.00 42.50 65.50 84.25 98.00 
Martin Schmelzer
  • 23,283
  • 6
  • 73
  • 98
  • ... %>% select(value) %>% quantile() works. Thanks. Stupid me. I did not know about the different pipe operators, very interesting. – tjebo Apr 08 '18 at 17:05