Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dplyr & r: Anonymous functions myst be parenthesized

Tags:

r

dplyr

I think I've stumbled apon my first error with a spelling mistake.

I am running the following code with R and dplyr.

> foobar = c(1,2,3)
> foobar %>% as.character
[1] "1" "2" "3"

This works fine, now I try to run it through an anonymous function.

> foobar %>% function(x) x * 2 
Error: Anonymous functions myst be parenthesized

Any idea what is happening? (And where I need to ping to get 'myst' corrected to 'must')?

like image 860
cantdutchthis Avatar asked Jan 21 '15 19:01

cantdutchthis


People also ask

What is dplyr used for?

dplyr is a grammar of data manipulation, providing a consistent set of verbs that help you solve the most common data manipulation challenges: mutate() adds new variables that are functions of existing variables. select() picks variables based on their names. filter() picks cases based on their values.

Why dplyr is used in R?

dplyr is a package for making data manipulation easier. Packages in R are basically sets of additional functions that let you do more stuff in R. The functions we've been using, like str() , come built into R; packages give you access to more functions.

What does dplyr stand for?

dplyr is a new package which provides a set of tools for efficiently manipulating datasets in R. dplyr is the next iteration of plyr , focussing on only data frames.

What are dplyr verbs R?

This article will cover the five verbs of dplyr: select, filter, arrange, mutate, and summarize.


1 Answers

The error message is quite informative (even if one word is misspelled). Put parentheses around the anonymous function.

foobar <- 1:3
foobar %>% (function(x) x * 2)
# [1] 2 4 6

For explanation, see the Using %>% with call- or function-producing rhs section in

help("%>%", "magrittr")

It has nothing to do with dplyr. As for the typo in the error message, whenever you find something that might need attention you can contact the package maintainer. Although it seems this has been fixed in the most recent development version of magrittr. An easy way to find the maintainer of a package is to use

maintainer("magrittr")

The result is omitted here because it contains an email address.

like image 123
Rich Scriven Avatar answered Sep 23 '22 13:09

Rich Scriven