0
list <- c(1,1,1,4,5,6,9,9,2)

I want to find the index of the 3 lowest values , but with the condition that the index of the found minima is at least 3 points apart from each other. To find the 3 lowest indices I'm using

which(list <= sort(list, decreasing=FALSE)[3], arr.ind=TRUE)

which doesn't look for any conditions and results in

1,2,3

My desired result is

1,9,4

I want to know if it's possible doing that without any loops since my list is a lot bigger.

Thank you so much in advance.

To clarify what I meant: The indices of minima must always be in a certain distance. For example for the list list<-c(1,3,9,5,9,9,2) the result of the minima should be 1,7,4. Not 1,7,2, because that the indices 1 and 2 are too close together. Thank you again for helping me.

  • The conditions are not very clear. Are you trying to find the index with the distance condition on the `sort`ed vector or the original one – akrun Nov 30 '16 at 13:08
  • I'm trying to find the index with the distance condition. –  Nov 30 '16 at 13:11
  • I meant whether the distance is in the origingal vector or sorted – akrun Nov 30 '16 at 13:12
  • the distance refers to the indices of the original vector. –  Nov 30 '16 at 13:13
  • Can't anyone help? I would really appreciate it since I am stuck. Thank you so much in advance. –  Dec 06 '16 at 10:37

1 Answers1

1

Try this using dplyr: create a dataframe with sequence in the 2nd column, then sort and find first occurance

library(dplyr)

kk <- data.frame(cbind(list, seq=seq_along(list))) %>% 
    arrange(list) %>%  # sort list
    group_by(list) %>% # group
    summarise(V3=min(seq)) %>% # find first occurance
    .$V3 %>% # get sequence values  
    head(3) # get top 3

[1] 1 9 4
OmaymaS
  • 1,671
  • 1
  • 14
  • 18
  • Thank you so much for the effort. There is a problem when the Minima occur in a different order. For example for `c(1,3,9,5,9,9,2)` I would expect **1,7,4** but I get **1,7,2** –  Dec 01 '16 at 13:32