删除行时删除了索引标签,我想解决这个问题吗?
Index labels removed when deleting rows, do I want to fix this?
我从我的 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),]
你可以这样做-
> library(data.table)
> subset(setDT(macdf,row.names),select=-rn)
或
rownames(macdf) <- NULL
您可以使用以下方法将标签从 "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)
我从我的 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),]
你可以这样做-
> library(data.table)
> subset(setDT(macdf,row.names),select=-rn)
或
rownames(macdf) <- NULL
您可以使用以下方法将标签从 "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)