Ifelse() with three conditions

前端 未结 3 1526
萌比男神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:26

    There is ambiguity in your question. Do you want different random values for all indexes where a==b or one random value for all indexes?

    The answer by @Rob will work in the second scenario. For the first scenario I suggest avoiding ifelse:

    u<-rep(NA,length(a))
    u[a>b] <- 1
    u[a

提交回复
热议问题