R How to Reset Index Numbers of Rows in Data Frame (Example Code)
This tutorial shows how to modify or reset all index numbers of a data frame in R programming.
data(iris) # Example data iris_new <- iris[c(77, 1, 55, 20, 6, 10), ] iris_new # Display example data in RStudio # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 77 6.8 2.8 4.8 1.4 versicolor # 1 5.1 3.5 1.4 0.2 setosa # 55 6.5 2.8 4.6 1.5 versicolor # 20 5.1 3.8 1.5 0.3 setosa # 6 5.4 3.9 1.7 0.4 setosa # 10 4.9 3.1 1.5 0.1 setosa |
data(iris) # Example data iris_new <- iris[c(77, 1, 55, 20, 6, 10), ] iris_new # Display example data in RStudio # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 77 6.8 2.8 4.8 1.4 versicolor # 1 5.1 3.5 1.4 0.2 setosa # 55 6.5 2.8 4.6 1.5 versicolor # 20 5.1 3.8 1.5 0.3 setosa # 6 5.4 3.9 1.7 0.4 setosa # 10 4.9 3.1 1.5 0.1 setosa
Example: Resetting Index Numbers of Data Frame Rows to Sequence from 1 to N
rownames(iris_new) <- NULL # Resetting index numbers of rows iris_new # Display updated index numbers and data # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 6.8 2.8 4.8 1.4 versicolor # 2 5.1 3.5 1.4 0.2 setosa # 3 6.5 2.8 4.6 1.5 versicolor # 4 5.1 3.8 1.5 0.3 setosa # 5 5.4 3.9 1.7 0.4 setosa # 6 4.9 3.1 1.5 0.1 setosa |
rownames(iris_new) <- NULL # Resetting index numbers of rows iris_new # Display updated index numbers and data # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 6.8 2.8 4.8 1.4 versicolor # 2 5.1 3.5 1.4 0.2 setosa # 3 6.5 2.8 4.6 1.5 versicolor # 4 5.1 3.8 1.5 0.3 setosa # 5 5.4 3.9 1.7 0.4 setosa # 6 4.9 3.1 1.5 0.1 setosa
Related Articles
In the following, you may find some further resources on topics such as vectors, factors, and dates: