Ifelse() with three conditions

前端 未结 3 1545
萌比男神i
萌比男神i 2021-01-01 18:46

I have two vectors:

a<-rep(1:2,100)

b<-sample(a)

I would like to have an ifelse condition that compares each value of a

3条回答
  •  自闭症患者
    2021-01-01 19:17

    How about adding another ifelse:

    ifelse(a>b, 1, ifelse(a==b, sample(1:2, length(a), replace = TRUE), 0))
    

    In this case you get the value 1 if a>b, then, if a is equal to b it is either 1 or 2 (sample(1:2, length(a), replace = TRUE)), and if not (so a must be smaller than b) you get the value 0.

提交回复
热议问题