Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

last() gives cryptic error message #2090

Closed
t-kalinowski opened this issue Aug 25, 2016 · 6 comments
Closed

last() gives cryptic error message #2090

t-kalinowski opened this issue Aug 25, 2016 · 6 comments
Labels
bug an unexpected problem or unintended behavior

Comments

@t-kalinowski
Copy link
Contributor

Hello!

a small example:

library(dplyr)
n <- 9
x <- c(ifelse(sample(c(T,F), n, T), 1:n, NA ), NA)
df <-  data_frame(x)

last(na.omit(x)) # works

mutate(df, y = last(na.omit(x)))            # doesn't work
# Error: Unsupported vector type language

mutate(df, z = last(x[!is.na(x)]))          # doesn't work
# Error: Unsupported vector type language

mutate(df, y = x %>% na.omit() %>% last() ) # doesn't work
# Error in mutate_impl(.data, dots) : bad value


mutate(df, y = x %>% na.omit %>% last ) # works
last(x[!is.na(x)]) # works

mutate(df, z = last(x)) # "works"

I really love dplyr and am immensely grateful for all the hard work that goes into it!

I'm not savvy enough to even diagnose what's leading to these error messages, but I suspect this is not intended behavior.

@py-b
Copy link

py-b commented Aug 25, 2016

Related to this ?
#1980

@krlmlr
Copy link
Member

krlmlr commented Nov 7, 2016

Thanks, confirmed. Would you like to contribute a testthat test?

This looks related to, but probably not the same as #1980.

@krlmlr krlmlr added bug an unexpected problem or unintended behavior data frame labels Nov 7, 2016
t-kalinowski added a commit to t-kalinowski/dplyr that referenced this issue Nov 12, 2016
@dholstius
Copy link

Similar behavior exposed via filter().

I would not have thought the two expressions below would yield different outcomes — since identical(6L, c(6L)) is TRUE — but they do:

> filter(mtcars, cyl == last(6L)) # FIXME: returns 0 rows
> filter(mtcars, cyl == last(c(6L))) # FIXME: throws "Unsupported vector type language"

In contrast to last, first yields the expected outcome:

# All of these yield the correct result
> filter(mtcars, cyl == first(6L))
> filter(mtcars, cyl == first(c(6L))) 
> filter(mtcars, cyl == 6L)

Feels like ... maybe an off-by-one in nth?

@dholstius
Copy link

Hi @krlmlr,

I'm not good at debugging Rcpp stuff — I only know how to insert stop() statements, load_all(), think, and repeat — but it seems that:

  1. Supplying order_by = NULL makes the problem disappear. For example, filter(mtcars, cyl == last(c(6L), order_by = NULL)) produces the correct result, while filter(mtcars, cyl == last(c(6L))) throws the error noted by @t-kalinowski ; and

  2. The "easy case" (nargs == 2, line 157 of hybrid_nth.cpp, switch(TYPEOF(data))) only handles INTSXP, REALSXP, STRSXP, and LGLSXP. So it falls through when TYPEOF(data) is language, which happens when order_by is not supplied.

Hope these observations are helpful.

krlmlr pushed a commit that referenced this issue Feb 8, 2017
* tests for first and last (#2090)

* address review comments

* address review comments

* oops

* only test last; rm whitespace

* address reviewer comments
@krlmlr
Copy link
Member

krlmlr commented Feb 10, 2017

This will be resolved by #2143, but that might take some time until it's ready.

@krlmlr krlmlr closed this as completed Feb 10, 2017
@BrazilForever11
Copy link

Here is another workaround

mutate(df,y = tail(na.omit(x),1))

@lock lock bot locked as resolved and limited conversation to collaborators Jun 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

5 participants