Search code examples
rindexingrowrowsdelete-row

Index labels removed when deleting rows, do I want to fix this?


I deleted rows from my R dataframe and now the index numbers are out of order. For example, the row-index was 1,2,3,4,5 before but now it is 2,3,4 because I deleted rows 1 and 5.

Do I want to change the index labels from 2,3,4 to 1,2,3 on my new dataframe?

If so, how do I do this? If not, why not?

library(rvest)

url <- "https://en.wikipedia.org/wiki/Mid-American_Conference"
pg <- read_html(url) # Download webpage
pg

tb <- html_table(pg, fill = TRUE) # Extract HTML tables as data frames
tb

macdf <- tb[[2]]


macdf <- subset(macdf, select=c(1,2,5))


colnames(macdf) <- c("School","Location","NumStudent")


macdf <- macdf[-c(1,8),]

Solution

  • You can change the labels from "2" "3" "4" "5" "6" "7" "9" "10" "11" "12" "13" "14" to "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" using: row.names(macdf) <- 1:nrow(macdf)