1

I have looked but I just don't understand why im getting this error. I want to allocate a number in CNSTATUS depending on the integer value in the column CN If less than 10, the CNSTATUS should be 10, if between 10 and 20 CNSTATUS shuold be 2 and if more than 20 it should be 3

CN is an integer column

My dataframe

CN       CNSTATUS
 2          
 12
 43
  2
  4

My code:

 if(OCCAMSdbTumCN$CN < 10) {
  OCCAMSdbTumCN$CNSTATUS <- 1
 }
 else if(OCCAMSdbTumCN$CN > 10 & OCCAMSdbTumCN$CN<20) {
   OCCAMSdbTumCN$CNSTATUS <- 2
 }
 else{
 OCCAMSdbTumCN$CNSTATUS <- 2}


 The error I am getting is

In if (OCCAMSdbTumCN$CN < 10) { :
  the condition has length > 1 and only the first element will be used
Sebastian Zeki
  • 6,690
  • 11
  • 60
  • 125

1 Answers1

2

You'll want something like this:

OCCAMSdbTumCN$CSTATUS <- ifelse(OCCAMSdbTumCN$CN < 10, 1, ifelse(OCCAMSdbTumCN$CN >= 10 & OCCAMSdbTumCN$CN < 20, 2, 3))

Play around with the equals or greater than or equal to as you need to. Note in your example, cases 10 and 20 won't be caught until the end.

goodtimeslim
  • 880
  • 7
  • 13