jeudi 29 août 2019

Getting the wrong result with if else r function with decimal variable [duplicate]

This question already has an answer here:

I am working in r with the following data.frame with aprox 40.000.000 rows:

structure(list(group = c(1003, 1003, 1003, 1003, 1003, 1003, 
1003, 1003, 1003, 1003), t_year = c("2014", "2014", "2014", "2014", 
"2014", "2014", "2014", "2014", "2014", "2014"), tmonth = c(3, 
3, 3, 3, 3, 3, 3, 3, 3, 3), tday = c("02", "02", "02", "02", 
"02", "02", "02", "02", "02", "02"), md = c(2507.416244074, 2507.416244074, 
2507.416244074, 2507.416244074, 2507.416244074, 2507.416244074, 
2507.416244074, 2507.416244074, 2507.416244074, 2507.416244074
), st = c(640722481.20599, 640722481.20599, 640722481.20599, 
640722481.20599, 640722481.20599, 640722481.20599, 640722481.20599, 
640722481.20599, 640722481.20599, 640722481.20599), bsc = c(255530.960493802, 
255530.960493802, 255530.960493802, 255530.960493802, 255530.960493802, 
255530.960493802, 255530.960493802, 255530.960493802, 255530.960493802, 
255530.960493802), animal = c("HOUSA000062901617", "HOUSA000006684687", 
"HO982000202967406", "HOUSA000057341913", "HOUSA000139926709", 
"JEUSA000057281350", "HOUSA000056634042", "XXUSA000056639940", 
"HOUSA000064279445", "HOUSA000066846844"), ln = c(6L, 2L, 1L, 
2L, 4L, 2L, 3L, 2L, 5L, 1L), gluc = c(37892.914163, 100000, 606286.6266, 
303143.3133, 303143.3133, 35355.339059, 37892.914163, 37892.914163, 
214354.69251, 37892.914163), gluc_cat = c(1L, 1L, 6L, 5L, 5L, 
1L, 1L, 1L, 4L, 1L), ol = structure(c(1L, 1L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L), .Label = c("mult", "prim"), class = "factor"), 
    group_size = c("<2000", "<2000", "<2000", "<2000", "<2000", 
    "<2000", "<2000", "<2000", "<2000", "<2000"), date = structure(c(16131, 
    16131, 16131, 16131, 16131, 16131, 16131, 16131, 16131, 16131
    ), class = "Date"), season = c("Spring", "Spring", "Spring", 
    "Spring", "Spring", "Spring", "Spring", "Spring", "Spring", 
    "Spring")), row.names = c(NA, 10L), class = "data.frame")

I need to create a factor of the variable "bsc" in the data.frame.

I used the ifelse function like bellow:

a <- seq(0:200000)
b <- seq(200000:400000)
c <- seq(400000:8000000)

a <- as.numeric(a)
b <-as.numeric(b)
c <- as.numeric(c)

df$bsc <- as.numeric(df$bsc)

df$bsc_cat <- ifelse(df$bsc %in% a, "Low",
                     ifelse(df$bsc %in% b, "Medium",
                            ifelse(df$bsc %in% c, "High",
                         "N")))

But I am getting only results "N". I imagined that it is happening because the bsc variable have decimal numbers. Because I used the same function to others variables without decimals and it worked well.

How do I fix it?

Aucun commentaire:

Enregistrer un commentaire