I have a list as follows:
alist <- list(0.147293560981687, 0.124073985832788, 0.071172828740332,
0.0650147970461593, 0.0706446776127474, 0.0851406172884224,
0.0915347157882129, 0.0964307208897466, 0.115968630433955,
0.12558846846113, 0)
I would like to extract the index of the lowest number non-zero number in the list, which if I am not mistaken is 4.
Because of the zero, I cannot do:
min( unlist(alist ))
How should I do this?
EDIT:
Showing the issue with the proposed answer of bpvalderrama, which works because the position of the 0
above does not influence the indexes in the subset. However if the (for example) the first value is 0
, it no longer works, because the indexes of the subset are no longer identical to the original list.
alist <- list(0, 0.124073985832788, 0.071172828740332,
0.0650147970461593, 0.0706446776127474, 0.0851406172884224,
0.0915347157882129, 0.0964307208897466, 0.115968630433955,
0.12558846846113, 0)
which.min(alist[alist>0])
[1] 3
CodePudding user response:
maybe you can try with :
which.min(alist[alist>0])
which will give you the following output :
[1] 4
EDIT :
Since Tom wanted to keep the 0 values in the vector (which I didn't notice), you can use :
alist <- list(0, 0.124073985832788, 0.071172828740332,
0.0650147970461593, 0.0706446776127474, 0.0851406172884224,
0.0915347157882129, 0.0964307208897466, 0.115968630433955,
0.12558846846113, 0)
which.min(replace(alist, alist<=0, NA))
And the 0 are still kept as position holders but not considered in the which.min()
call since now they are NA