Dplyr row_number () behavior in boolean operation

While answering another question , I came across a behavior dplyr::row_number()

, which was a bit surprising to me, and I wonder if this should be the behavior and what the reason is for. Here's an example: I want to make two logical tests related to OR or AND using a function row_number()

. Here's a simple example that doesn't make a lot of sense, but illustrates what I mean:

library(dplyr)
iris %>%
  mutate(newcol = row_number() - Sepal.Length < 0 | row_number() - Petal.Length < 0)
Error in rank(x, ties.method = "first") : 
  argument "x" is missing, with no default

      

As you can see, it returns an error. Executing each part of the logic test individually performs well. And to solve the problem, I need to add a column that the second should refer to row_number

, for example, both of these modifications work:

iris %>%
  mutate(newcol = row_number() - Sepal.Length < 0 | row_number(Species) - Petal.Length < 0)

      

Or

iris %>%
  mutate(newcol = row_number(Species) - Sepal.Length < 0 | row_number(Species) - Petal.Length < 0)

      

I wonder if this is a software necessity or not, or why it row_number

doesn't just use the same input after |

as before. Can anyone help me figure this out?

(I am running dplyr

0.2, R version 3.1.0 (2014-04-10), RStudion Version 0.98.977)

Edit:

You can also demonstrate here:

iris %>% mutate(test = 1 == 1 | row_number() < 10)
iris %>% mutate(test = 1 == 1 | row_number(Species) < 10)
iris %>% mutate(test = row_number() < 10 | 1 == 1)

      

Edit 2:

Adding to my confusion is that

iris %>% mutate(test = TRUE | row_number() < 10)  # works
iris %>% mutate(test = FALSE | row_number() < 10) # also works

      

but

iris %>% mutate(test = 1 == 1 | row_number() < 10) # throws error
iris %>% mutate(test = 1 == 2 | row_number() < 10) # also throws error

      

+3


source to share


1 answer


This issue has been closed by this commit .

the current version available on CRAN at the time of this reply is version 0.3.0.2. This version demonstrates the behavior you expect.



temp <- iris %>%
  mutate(newcol = row_number() - Sepal.Length < 0 | 
           row_number() - Petal.Length < 0)
head(temp)
#   Sepal.Length Sepal.Width Petal.Length Petal.Width Species newcol
# 1          5.1         3.5          1.4         0.2  setosa   TRUE
# 2          4.9         3.0          1.4         0.2  setosa   TRUE
# 3          4.7         3.2          1.3         0.2  setosa   TRUE
# 4          4.6         3.1          1.5         0.2  setosa   TRUE
# 5          5.0         3.6          1.4         0.2  setosa  FALSE
# 6          5.4         3.9          1.7         0.4  setosa  FALSE

      

+2


source







All Articles