Multiple true condition in ifelse command

前端 未结 2 1644
-上瘾入骨i
-上瘾入骨i 2021-01-17 06:43

In R we use ifelse(test, yes, no) command. The problem which i am facing is if a codiation comes out to be true i need to perform various statement, for example

相关标签:
2条回答
  • 2021-01-17 07:18

    This works:

    a <- "01" 
    ifelse(a=="01", d <- c(sum(b),sum(c)), d <- 0)
    d
    #[1]   21 2616
    
    a <- "02"
    ifelse(a=="01", d <- c(sum(b),sum(c)), d <-0)
    d
    #[1] 0
    
    0 讨论(0)
  • 2021-01-17 07:34

    You can use the & ('and') statement. You also need to alocate the sum to the variable, like this:

    a <- "01" 
    b <- c(1,2,3,4,5,6) 
    c <- c(12,13,1234,1334,23) 
    d <- ifelse(a=="01",( (b <- sum(b)) & (c <- sum(c)) ),0)
    
    0 讨论(0)
提交回复
热议问题