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.
- Create Row number or Row index of the dataframe in R using seq.int() function
- Generate row number of the Dataframe using row_number() and also by 1:n()
- Generate row numbers by group with an example in R
Let’s first create the dataframe
#### Create dataframe my_basket = data.frame(ITEM_GROUP = c("Fruit","Fruit","Fruit","Fruit","Fruit","Vegetable","Vegetable","Vegetable","Vegetable","Dairy","Dairy","Dairy","Dairy","Dairy"), ITEM_NAME = c("Apple","Banana","Orange","Mango","Papaya","Carrot","Potato","Brinjal","Raddish","Milk","Curd","Cheese","Milk","Paneer"), Price = c(100,80,80,90,65,70,60,70,25,60,40,35,50,120), MRP = c(101,85,85,96,67,73,65,71,33,64,45,36,54,123)) my_basket
so the dataframe my_basket will be
Generate Row number or Row index to the dataframe using seq.int():
Row number is generated and stored in a column using seq.int() function
#### Generate row number or row index using seq.int() function my_basket$row_num <- seq.int(nrow(my_basket)) my_basket
so the resultant dataframe with row number or row index generated will be
Generate Row number or Row index to the dataframe using row_number():
Row number is generated and stored in a column using row_number() function. row_number() of the dplyr package is used along with mutate function in order to generate the row number as follows
#### Generate row number or row index using row_number() function library(dplyr) mutate(my_basket, id = row_number())
so the resultant dataframe with row number or row index generated and stored in the name of id
Generate Row number or Row index to the dataframe using 1:n():
Row number is generated and stored in a column using 1:n() function. 1:n() of the dplyr package is used along with mutate function in order to generate the row number as follows
#### Generate row number or row index using 1:n() library(dplyr) my_basket %>% mutate(row_number= 1:n())
so the resultant dataframe with row number or row index generated and stored in the name of row_number
Generate Row number of the dataframe by group
Row number is generated and stored in a column using 1:n() function. 1:n() of the dplyr package also used to generate the row number by group by using group_by() function. In below example the row numbers are generated by is “ITEM_GROUP”. “ITEM_GROUP” is passed as an argument to the group_by() function.
#### Generate row number by group library(dplyr) My_data <- my_basket %>% group_by(ITEM_GROUP) %>% mutate(row_number_bygroup = 1:n())
so the resultant dataframe with row numbers generated and grouped by “ITEM_GROUP” group column is shown