tidyft icon indicating copy to clipboard operation
tidyft copied to clipboard

Error with filter() when using lapply()

Open lluisfortes1 opened this issue 5 years ago • 2 comments
trafficstars

I want to indicate, I've found both tidyft and poorman package throws the same error with the same calculus

The basic use of filter() works as expected:

library(tidyft)
a = as.data.table(iris)

sp1 = 'virginica'
filter(a, Species == sp1)

But when I want to create a list based on the factor level Species, it fails

sp2 = levels(a$Species)

lapply(sp2, function(x) {
  filter(a, Species == x)
})

Same if factor is used as character variable:

sp3 = unique(as.character(a$Species))

lapply(sp3, function(x) {
  filter(a, Species == x)
})

When using with dplyr, it's working correctly:

lapply(sp2, function(x) {
  dplyr::filter(a, Species == x)
})

lapply(sp3, function(x) {
  dplyr::filter(a, Species == x)
})

lluisfortes1 avatar Apr 15 '20 13:04 lluisfortes1

Thank you for reporting, for this case, please use:

library(tidyft)

a = as.data.table(iris)

split(a,by = "Species")

hope-data-science avatar Apr 15 '20 13:04 hope-data-science

Awesome!! Thanks for your work

lluisfortes1 avatar Apr 15 '20 13:04 lluisfortes1