Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

indexing

r

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
like image 245
Rhino Avatar asked Jan 28 '23 09:01

Rhino


1 Answers

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 reverse 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"))
like image 117
markus Avatar answered Feb 03 '23 06:02

markus