5

I need to assign values in the variable "TRTCD1" in two different classes as 1 and 2, based on the condition as stated in the R programming code below.

z$Treatment1.class<-with(z, ifelse(TRTCD1 == 0 & TRTCD1 == 40, 1,
    ifelse(TRTCD1 >= 10 & TRTCD1 <= 30 & TRTCD1 == 50, 2)))

On running these code, I am getting the error:

Argument "No" is missing, with no default

Here, Treatment1.class is a new variable in table z which holds the output of the class.

How to fix this issue?

Pang
  • 9,564
  • 146
  • 81
  • 122
Mukhtar Ahmed
  • 61
  • 1
  • 2
  • 5
  • 4
    `ifelse()` needs three parameters: test, truth value, false value. Your inner `ifelse()` only has two parameters. You must specify a value when the test is false. It's unclear what you were trying to do with your code. You should describe your intent with words. – MrFlick Apr 20 '15 at 01:15
  • Basically i want to classify the values in TRTCD1 variable based on the following condition. If TRTCD1 is equal to 0 and 40 it should be grouped as 1. TRTCD1 greater than equal to 10 and less than equal to 30 and equal to 50 to be grouped as 2. I tried specifying the false value as 3, but it didn't produce the correct output. All the values were grouped as false (i.e. 3). Let me know if there is a way to solve this issue. – Mukhtar Ahmed Apr 20 '15 at 02:02
  • 1
    Please provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data so we can test possible solutions. That *should* work so there bust be something wrong with your data. – MrFlick Apr 20 '15 at 02:25
  • Please find the sample input data below. TRTCD1<- c( 0 0 0 0 10 10 0 10 0 0 0 0 0 0 0 0 0 10 10 10 0 0 0 10 10 10 0 0 0 10 0 0 0 10 10 10 0 0 0 0 0 0 0 20 10 0 0 0 0 0 0 0 0 10 0 0 0 0 0 10 10 0 30 0 10 0 50) – Mukhtar Ahmed Apr 20 '15 at 02:29
  • Edit your original question with the data since it's difficult to properly format in the comments. If possible, use a `dput()` like described in the link I provided. – MrFlick Apr 20 '15 at 02:30
  • How `TRTCD1 >= 10 & TRTCD1 <= 30 & TRTCD1 == 50` could be true? –  Apr 20 '15 at 02:32
  • data$dput<-with(z, ifelse(TRTCD1 == 0 & TRTCD1 == 40, 1, ifelse(TRTCD1 >= 10 & TRTCD1 <= 30 & TRTCD1 == 50, 2,3))) – Mukhtar Ahmed Apr 20 '15 at 02:33
  • Basically the data in TRTCD1 represents the different treatments in forest plot. 10,20, 30 and 50 have treatment in them. Inorder to group them as treated plots i used TRTCD1 >= 10 & TRTCD1 <= 30 & TRTCD1 == 50. On other hand 0 and 40 has no treatment effect, so i thought of grouping it as 2. Values which do not fall in these two cases are considered as false. – Mukhtar Ahmed Apr 20 '15 at 02:41
  • So you need `TRTCD1 >= 10 & TRTCD1 <= 30 | TRTCD1 == 50`. –  Apr 20 '15 at 02:43
  • I forgot to mention OR operator. Now i am able to get the correct output. Thank you Guys. – Mukhtar Ahmed Apr 20 '15 at 02:46

2 Answers2

4

My guess is as following.

ifelse(TRTCD1 == 0 & TRTCD1 == 40,
       1,
       ifelse(TRTCD1 >= 10 & TRTCD1 <= 30 & TRTCD1 == 50, 2, *value if NO*)
)

Only the case where the second ifelse is TRUE is given.

Jaehyeon Kim
  • 1,328
  • 11
  • 16
  • I am getting the false value (i.e. if no) even if the condition is true. – Mukhtar Ahmed Apr 20 '15 at 02:13
  • Looks like you already solved it with @Pascal's advice: your condition was unsatisfiable, it needed to be `TRTCD1 >= 10 & TRTCD1 <= 30 | TRTCD1 == 50` – smci Apr 20 '15 at 04:18
0

This Error tells that else statement is missing which should be written as:

z$Treatment1.class<-with(z, ifelse(TRTCD1 == 0 & TRTCD1 == 40, 1,
    ifelse(TRTCD1 >= 10 & TRTCD1 <= 30 & TRTCD1 == 50, 2,<else code here>)))

ajay
  • 43
  • 6