Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does mutate(row_number()) fail for data.tables?

When I try to use row_number() from dplyr on a data.table, it throws an error. Here's an example:

library(dplyr)
library(data.table)
mine <- data.table(a = 1:10)
mine %>% mutate(row_number())
# Error in rank(x, ties.method = "first", na.last = "keep") : 
#   argument "x" is missing, with no default

Any ideas why this is happening?

like image 580
Jake Fisher Avatar asked Feb 15 '16 20:02

Jake Fisher


People also ask

What does row_ number do in r?

The row_number() function is useful for creating an identification number (an ID variable). It is also useful for labeling each observation by a grouping variable. Using the practice dataset, let's add a variable called Session . Each session is comprised of 1 positive day and 1 negative day closest in date.

How to create a row number in r?

To Generate Row number to the dataframe in R we will be using seq.int() function. Seq.int() function along with nrow() is used to generate row number to the dataframe in R. We can also use row_number() function to generate row index. We will also focus on generating row numbers by group with an example.


1 Answers

The dplyr::row_number() function has a mandatory parameter, which should be the name of the column that you want to do a row number for.

In your example, you should have written it like this:

library(dplyr)
library(data.table)
mine <- data.table(a = 1:10) %>% 
    mutate(row_number(a))

Because a is the name of the column that you want to add the row_number() for. Without this, then R throws the error argument "x" is missing, with no default.

However, I'd suggest using the tibble::rowid_to_column() function. It's cleaner.

library(dplyr)
library(data.table)
library(tibble)
mine <- data.table(a = 1:10) %>% 
    rowid_to_column()

I hope that helps.

like image 94
chrimaho Avatar answered Sep 20 '22 10:09

chrimaho