nathaneastwood / poorman

A poor man's dependency free grammar of data manipulation
https://nathaneastwood.github.io/poorman/
Other
339 stars 15 forks source link

Error with filter() when using lapply() #9

Closed lluisfortes1 closed 4 years ago

lluisfortes1 commented 4 years ago

The basic use of filter() works as expected:

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

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

sp2 = levels(iris$Species)

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

Same if factor is used as character variable:

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

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

When using with dplyr, it's working correctly:

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

lapply(sp3, function(x) {
  dplyr::filter(iris, Species == x)
})
nathaneastwood commented 4 years ago

Good spot, I'll take a look

nathaneastwood commented 4 years ago

For now you can use

split(iris, iris$Species)

But I will see if I can come up with something.