HaXoR - 1 year ago 61

R Question

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

ifelse(fp$month==1,(fp$sum(sales_1),fp$sum(sales_2)),0)

So I am giving two condition if fp$month = 1.those conditions are **sum(fp$sales_1),sum(fp$sales_2)**, but R is recognising second comma. How to give multiple true condiations?

the example is

`a <- "01"`

b <- c(1,2,3,4,5,6)

c <- c(12,13,1234,1334,23)

d <- ifelse(a=="01",(sum(b),sum(c)),0)

here i want if the "a" value is "01" then i want sum of vector b and c. but i am getting error :-

`Error: unexpected ',' in "d <- ifelse(a=="01",(sum(b),"_`

The expected output is :-

`[1] 21 2616`

Answer Source

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
```