1

I am trying to to create a category for my elevation variable (like a histogram bin), but I can't get my if else statement to work. It will not accept two ifs, but they are needed in order to make intervals for x, like: 1200m < x > 1300m.

This is what I've tried - and I have tried with both single and double '&':

NGS$Ecat <- ""
  if(NGS$Elevation <1100){NGS$Ecat <- 1} else if(NGS$Elevation <1200 && >1100){NGS$Ecat <- 2} else if(NGS$Elevation <1300 && >1200){NGS$Ecat <- 3} if(NGS$Elevation <1400 && >1300){NGS$Ecat <- 4} else if(NGS$Elevation <1500 && >1400){NGS$Ecat <- 5} else if(NGS$Elevation <1600 && >1500){NGS$Ecat <- 6} else if(NGS$Elevation <1700 && >1600){NGS$Ecat <- 7} else if(NGS$Elevation <1800 && >1700){NGS$Ecat <- 8}else if(NGS$Elevation <1900 && >1800){NGS$Ecat <- 9} else if(NGS$Elevation <20000 && >1900){NGS$Ecat <- 10} else {NGS$Ecat <- NA} }

This is the error I get:

Error: unexpected '>' in "if(NGS$Elevation <1100){NGS$Ecat <- 1} else if(NGS$Elevation <1200 && >"

Here is a part of the data:

data <-  structure(list(samples = structure(1:20, .Label = c("1_cl_A", 
        "1_cl_B", "1_cl_C", "1_op_A", "1_op_C", "2_cl_A", "2_cl_B", "2_cl_C", 
        "2_op_A", "2_op_B", "2_op_C", "3_cl_A", "3_cl_C", "3_op_A", "3_op_B", 
        "3_op_C", "4_cl_A", "4_cl_C", "4_op_A", "4_op_B", "4_op_C", "5_cl_A", 
        "5_cl_B", "5_cl_C", "5_op_B", "5_op_C", "6_cl_A", "6_cl_B", "6_cl_C", 
        "6_op_A", "6_op_B", "6_op_C", "7_cl_A", "7_cl_B", "7_cl_C", "7_op_B", 
        "8_cl_A", "8_cl_B", "8_cl_C", "8_op_B", "8_op_C"), class = "factor"), 
            Enr = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 
            3L, 3L, 3L, 4L, 4L, 4L, 4L), opcl = structure(c(1L, 1L, 1L, 
            2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 
            2L, 2L), .Label = c("cl", "op"), class = "factor"), rep = structure(c(1L, 
            2L, 3L, 1L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 3L, 1L, 2L, 3L, 
            1L, 3L, 1L, 2L), .Label = c("A", "B", "C"), class = "factor"), 
            Reads = c(18856L, 27189L, 13064L, 18047L, 34369L, 11942L, 
            27612L, 10127L, 26538L, 36421L, 23625L, 23637L, 9395L, 24582L, 
            11602L, 28096L, 12883L, 29031L, 21071L, 16105L), OTUs = c(55L, 
            88L, 28L, 47L, 52L, 32L, 70L, 73L, 73L, 66L, 89L, 48L, 94L, 
            62L, 69L, 54L, 42L, 52L, 70L, 45L), Elevation = c(1200L, 
            1202L, 1183L, 1200L, 1203L, 1295L, 1319L, 1295L, 1278L, 1294L, 
            1290L, 1309L, 1276L, 1298L, 1288L, 1300L, 1420L, 1387L, 1353L, 
            1375L), Vegetation = structure(c(4L, 4L, 4L, 3L, 3L, 4L, 
            4L, 4L, 1L, 1L, 1L, 4L, 4L, 3L, 3L, 3L, 2L, 2L, 1L, 1L), .Label = c("clearing", 
            "conifer forest", "meadow", "mix forest", "open pine"), class = "factor")), .Names = c("samples", 
        "Enr", "opcl", "rep", "Reads", "OTUs", "Elevation", "Vegetation"
        ), row.names = c(NA, 20L), class = "data.frame")
Mathilde
  • 191
  • 1
  • 11
  • 3
    You can't just put `NGS$Elevation <20000 && >1900` in R, you have to repeat the variable : `NGS$Elevation <20000 && NGS$Elevation >1900` (or you can use `between(NGS$Elevation, 1900, 20000)` as an alternative. Moreover, you should use `case_when()` from the package `dplyr`; this way your conditions would be less confusing – MBB Aug 23 '17 at 12:47
  • 3
    This condition is lengthy and prone to errors like the one you see. You should probably work out a solution with `cut`. – lmo Aug 23 '17 at 12:47
  • 1
    once you've properly rewritten the statement you'll probably get a warning and unexpected results due to the fact that `if` does not work with `vectors` of length more than 1. As adviced, go for `cut` – Cath Aug 23 '17 at 12:51
  • @MBB Ahh yes of cause, you are right, this is the problem! thank you. And @ lmo you are also right, as there are several typos in that example. – Mathilde Aug 23 '17 at 12:54
  • 1
    Okay, thanks for directing me to that other question, sorry I hadn't found it myself. FYI, I fixed my command and put it into a 'for' loop and now it works. thank you all! – Mathilde Aug 23 '17 at 13:03

0 Answers0