Search code examples
rfunctionmagrittr

Using the pipe in unique() function in r is not working


I have some troubles using the pipe operator (%>%) with the unique function.

df = data.frame(
  a = c(1,2,3,1),
  b = 'a')

unique(df$a) # no problem here
df %>% unique(.$a) # not working here
# I got "Error: argument 'incomparables != FALSE' is not used (yet)"

Any idea?


Solution

  • As other answers mention : df %>% unique(.$a) is equivalent to df %>% unique(.,.$a).

    To force the dots to be explicit you can do:

    df %>% {unique(.$a)}
    # [1] 1 2 3
    

    An alternative option from magrittr

    df %$% unique(a)
    # [1] 1 2 3
    

    Or possibly stating the obvious:

    df$a %>% unique()
    # [1] 1 2 3