删除行时删除了索引标签,是否要修复此问题?

问题描述 投票:1回答:2

我从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),]
r indexing row rows delete-row
2个回答
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)


0
投票

你可以这样做 -

> library(data.table)
> subset(setDT(macdf,row.names),select=-rn)

要么

rownames(macdf) <- NULL
© www.soinside.com 2019 - 2024. All rights reserved.