为什么R中的if else语句不起作用?

时间:2017-08-23 12:45:04

标签: r if-statement

我正在尝试为我的高程变量创建一个类别(如直方图bin),但我无法使if else语句生效。 它不会接受两个ifs,但是为了制作x的间隔需要它们,例如:1200m< x> 1300米。

这就是我尝试过的 - 我尝试了单人和双人'&':

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

这是我得到的错误:

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

以下是数据的一部分:

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")

0 个答案:

没有答案