问题
Is it possible to get the original index of a data frame after subsetting? It is being stored somewhere but I am not sure where and how to access it. I understand that there is a better solution if this is part of the algorithm design. I am just curious if anyone knows if it possible.
Example Scenario:
df = data.frame(atr1=integer(),atr2=integer())
for(i in 1:10) {
df <- rbind(df,data.frame(atr1=as.integer(i),atr2=as.integer(i)))
}
View(df)
Note the far left side of output of View function in R studio will show the index (I am not sure how to post an image that only exists on my local machine). Create a data frame by taking subset of that original data frame:
df_subset <- df[which(df$atr1 > 4),]
View(df_subset)
The output of the View function doesn't index df_subset 1 to 6 as you would access them. The original indices 5 to 10 are maintained. I am curious if it is possible to accesses those indices in some fashion similar to:
df_subset[index,]$<some hidden attribute>
来源:https://stackoverflow.com/questions/57261986/r-get-the-original-index-of-data-frame-after-subsetting