R:在同一列中查找匹配值的索引

R:在同一列中查找匹配值的索引,r,indexing,R,Indexing,这给我今天带来了很多麻烦,我相信有一个明显的解决办法我没有想到 我有一个几千行的数据框。有一列,其中该列中的每个值正好显示两次。我想找到每个匹配值的索引。该列看起来是这样的: col 1 cat 2 dog 3 bird 4 dog 5 bird 6 cat 我想知道匹配出现的相应索引,它将返回如下内容: [1] 6 4 5 2 3 1 我们能做到 df$new_col <- seq_along(df$col) df$new_col <- with(df, ave(new_

这给我今天带来了很多麻烦,我相信有一个明显的解决办法我没有想到

我有一个几千行的数据框。有一列,其中该列中的每个值正好显示两次。我想找到每个匹配值的索引。该列看起来是这样的:

  col
1 cat
2 dog 
3 bird
4 dog
5 bird
6 cat
我想知道匹配出现的相应索引,它将返回如下内容:

[1] 6 4 5 2 3 1
我们能做到

df$new_col <- seq_along(df$col)
df$new_col <- with(df, ave(new_col, col, FUN = rev))
df
#   col new_col
#1  cat       6
#2  dog       4
#3 bird       5
#4  dog       2
#5 bird       3
#6  cat       1
数据

df <- structure(list(col = c("cat", "dog", "bird", "dog", "bird", "cat"
)), .Names = "col", class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6"))
df
df <- structure(list(col = c("cat", "dog", "bird", "dog", "bird", "cat"
)), .Names = "col", class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6"))