tidyverse pipe throws an error "comparison (6) is possible only for atomic and list types"

I am trying to select a subset of rows from a tibble by their row-wise sum values using a tidyverse pipe. Below is an example code.

library(tidyverse)
mytbl = tibble(a = 1:5, b = 1:5)
mytbl %>% .[(. %>% rowSums) > 5, ]

But this code throws a weird error.

#Error in (. %>% rowSums) > 5 : 
#   comparison (6) is possible only for atomic and list types

What does this mean and why am I getting this?

I'm not quite sure why the error is being thrown - I think the . argument isn't being used properly somehow. Here's some other ways you might do what I think you're trying to do:

library(tidyverse)
mytbl = tibble(a = 1:5, b = 1:5)


mytbl %>% .[rowSums(.) > 5 , ]
#> # A tibble: 3 x 2
#>       a     b
#>   <int> <int>
#> 1     3     3
#> 2     4     4
#> 3     5     5
mytbl %>%
  filter(rowSums(.) > 5)
#> # A tibble: 3 x 2
#>       a     b
#>   <int> <int>
#> 1     3     3
#> 2     4     4
#> 3     5     5

Created on 2020-09-30 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.