我从R数据框中删除了行,现在索引号不正确了。例如,行索引以前是1,2,3,4,5,但现在是2,3,4,因为我删除了行1和5。
我是否要在新数据框中将索引标签从2,3,4更改为1,2,3?
如果是,该怎么办? 如果没有,为什么不呢?
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),]
答案 0 :(得分:0)
您可以执行以下操作-
> library(data.table)
> subset(setDT(macdf,row.names),select=-rn)
OR
rownames(macdf) <- NULL
答案 1 :(得分:0)
您可以使用以下方法将标签从"2" "3" "4" "5" "6" "7" "9" "10" "11" "12" "13" "14"
更改为"1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12"
:
row.names(macdf) <- 1:nrow(macdf)