Reputation: 1
With gapminder being a tibble and lifeExp being a variable name in gapminder, I tried using two functions arrange() and desc().
This works:
gapminder %>% arrange(desc(lifeExp))
This does not work:
gapminder %>% desc(arrange(lifeExp)
Error: unused argument (arrange(lifeExp))
I am not sure why, any help with be much appreciated.
Upvotes: 0
Views: 384
Reputation: 632
The previous answer is more or less right but the reason for this is due to the nature of the piping operator %>%.
The pipe takes whatever is to the left and passes it as the first argument to the function on the right. If you look at the documentation for dplyr::desc
you can see it only takes a single argument x (the vector to be arranged), so your second example is equivalent to
desc(gapminder, arrange(lifeExp))
and R is seeing two arguments where it expects one (hence the error message).
arrange()
on the other hand accepts two arguments, so your first example which is equivalent to:
arrange(gapminder, desc(lifeExp))
works, as R is expecting two arguments to arrange()
and that is what it sees.
Upvotes: 1
Reputation: 11
I'm also new on this, but it seems that the grammar is different in each line of code.
gapminder %>% arrange(desc(lifeExp)) # is read like "the tibble gapminder", "then" "arrange" it "in descendant values" of "lifeExp"
gapminder %>% desc(arrange(lifeExp) # is read like "the tibble gapminder", "then" "in descendant order" "arrange it" by "lifeExp". The arrange command is for a tibble, while a desc is for aiming at one variable.
Espero me haya hecho entender bro
Upvotes: 1