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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With