cavaunpeu
cavaunpeu

Reputation: 644

Finding Indices of Unique Elements in R

Brain not working well today. I have a data frame as follows:

  V1   V2
1  8  200
2  8  200
3  8  200
4  8  200
5 34  250
6  8 2500

I'd like to return a list, with each element of the list containing a vector of the indices of each unique pair of elements in my data frame. The list should look like:

[[1]]
[1] 1 2 3 4

[[2]]
[1] 5

[[3]]
[1] 6

I've referenced this thread - quite similar - yet still stumped.

Thanks in advance SO.

Upvotes: 0

Views: 1754

Answers (3)

MrFlick
MrFlick

Reputation: 206606

Here's an option using tapply and interaction assuming your data.frame is named dd

with(dd, tapply(seq.int(nrow(dd)), interaction(V1, V2, drop=T), 
    identity, simplify=F))

# $`8.200`
# [1] 1 2 3 4
# 
# $`34.250`
# [1] 5
# 
# $`8.2500`
# [1] 6

Upvotes: 0

agstudy
agstudy

Reputation: 121618

Splitting by a combination the 2 columns:

split(seq_len(nrow(dat)),paste(dat$V1,dat$V2,sep='/'))

$`34/250`
[1] 5

$`8/200`
[1] 1 2 3 4

$`8/2500`
[1] 6

Upvotes: 1

Thomas
Thomas

Reputation: 44575

Assuming mydf is your dataframe, and that you're okay with character values being returned for your rownames, you can try:

> lapply(split(mydf, list(mydf$V1, mydf$V2), drop=TRUE), row.names)
$`8.200`
[1] "1" "2" "3" "4"

$`34.250`
[1] "5"

$`8.2500`
[1] "6"

Otherwise a little wordier:

> lapply(split(mydf, list(mydf$V1, mydf$V2), drop=TRUE),
         function(x) as.numeric(row.names(x)))
$`8.200`
[1] 1 2 3 4

$`34.250`
[1] 5

$`8.2500`
[1] 6

Note: Of course, if you have rownames that do not correspond to the row number in your original dataframe, you can't use these as extraction indices.

Upvotes: 4

Related Questions