简体   繁体   中英

R: Finding the index of a matching value within the same column

This has been giving me a lot of trouble today and I'm sure there is an obvious solution I'm not thinking of.

I have a data frame of a few thousand rows. There is a column where each value in that column appears exactly twice. I want to locate the index of each matching value. The column looks like so:

  col
1 cat
2 dog 
3 bird
4 dog
5 bird
6 cat

And I would like to know the corresponding index where the match appears so it would return something like this:

[1] 6 4 5 2 3 1

We can do

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

In the first step we create a new_col as a sequence running from 1 to nrow(df) . So this variable is not different from the row numbers.

If we think of variable col as defining groups, we get can get 'the corresponding index where the match appears' if we rev erse the newly created column by groups of col to get desired output.

As a oneliner

with(df, ave(seq_along(col), col, FUN = rev))

data

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"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM