R get the original index of data frame after subsetting

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-29 11:23:46

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!