Getting index of first occurrence of a value in every column of a matrix

前端 未结 4 1337
野性不改
野性不改 2021-02-02 16:06

If I have a single vector, I can get the 1st occurrence which is below a value:

test <- c(0.5,0.8,0.1,0.08,0.06,0.04)
which(test<0.1)[1]    
4条回答
  •  星月不相逢
    2021-02-02 16:40

    Here's another answer. Assuming you mean test2 where you write test3, note that 'test2<5' is a logical vector. The minimum value will be FALSE. The maximum value (TRUE) is what you want:

    > apply(test2<5,2,which.max)
    [1] 3 4
    

    Note that this is not correct if the maximum value is not TRUE.

提交回复
热议问题