1

I want to make a table on Hazard level. if the risk level is less than 10 then it will be hazard level "I", risk level<100 = II, risk level<1000 = III and risk level > 1000 = IV. Now for this run the code below. At first I made an empty column

Hazard_level <- c()

Then I make the statement like below

for(i in 1:lenght(data$Risk_value)){
  if(data$Risk_value[i] < 10){
    Hazard_Level <- append(Hazard_Level, 'I')
  }
  elseif(data$Risk_value[i] < 100){
    Hazard_Level <- append(Hazard_Level, 'II')
  }
  elseif(data$Risk_value[i] < 1000){
    Hazard_Level <- append(Hazard_Level, 'III')
  }
  else{
    Hazard_Level <- append(Hazard_Level, 'IV')
  }
}
df <- as.data.frame(Hazard_Level)
df1 <- cbind(data,df)

the error it showing was

Error: unexpected symbol in:
> for(i in 1:lenght(data$Risk_value)){
+   if(data$Risk_value[i] < 10){
+     Hazard_Level <- append(Hazard_Level, 'I')
+   }
+   elseif(data$Risk_value[i] < 100){
Error: unexpected '{' in:
"  }
  elseif(data$Risk_value[i] < 100){"
>     Hazard_Level <- append(Hazard_Level, 'II')
>   }
Error: unexpected '}' in "  }"
>   elseif(data$Risk_value[i] < 1000){
Error: unexpected '{' in "  elseif(data$Risk_value[i] < 1000){"
>     Hazard_Level <- append(Hazard_Level, 'III')
>   }
Error: unexpected '}' in "  }"
>   else{
Error: unexpected 'else' in "  else"
>     Hazard_Level <- append(Hazard_Level, 'IV')
>   }
Error: unexpected '}' in "  }"
> }
Error: unexpected '}' in "}"
> df1 = cbind(data,df)
Error in data.frame(..., check.names = FALSE) : 
  arguments imply differing number of rows: 23, 24

Data i am using was

structure(list(Station = c("LJDZ05", "LJDZ06", "LJDZ07", "LJDZ08", 
"LJDZ09", "LJDZ11", "LJDZ17", "LJDZ18", "LJDZ19", "LJDZ20", "LJDZ21", 
"LJDZ23", "LJDZ25", "LJDZ36", "LJDZ38", "LJDZ39", "LJDZ40", "LJDZ42", 
"LJDZ44", "LJDZ51", "LJDZ52", "LJDZ54", "LJDZ56"), Risk_value = c(26.62730994, 
1.068631579, 0.672, 3.407157895, 56.34514286, 0.584571429, 9.621879699, 
2.343035446, 2.177154135, 2.235609023, 6.438646617, 14.20606015, 
44.53034586, 22.44414608, 37.19215489, 33.58984127, 13.11310276, 
12.42682707, 89.37153383, 63.10576441, 81.76046115, 13.11488487, 
40.82847118)), class = "data.frame", row.names = c(NA, -23L)
Kazi
  • 67
  • 7

3 Answers3

2

We could use case_when function from dplyr package:

library(dplyr)
df %>% 
    mutate(Risk_level = case_when(Risk_value < 10 ~ 'I',
                                 Risk_value >=  10 & Risk_value < 100 ~ 'II',
                                 Risk_value >= 100 & Risk_value < 1000 ~ 'III',
                                 Risk_value >= 1000 ~ 'IV'))

Output:

   Station Risk_value Risk_level
1   LJDZ05 26.6273099         II
2   LJDZ06  1.0686316          I
3   LJDZ07  0.6720000          I
4   LJDZ08  3.4071579          I
5   LJDZ09 56.3451429         II
6   LJDZ11  0.5845714          I
7   LJDZ17  9.6218797          I
8   LJDZ18  2.3430354          I
9   LJDZ19  2.1771541          I
10  LJDZ20  2.2356090          I
11  LJDZ21  6.4386466          I
12  LJDZ23 14.2060602         II
13  LJDZ25 44.5303459         II
14  LJDZ36 22.4441461         II
15  LJDZ38 37.1921549         II
16  LJDZ39 33.5898413         II
17  LJDZ40 13.1131028         II
18  LJDZ42 12.4268271         II
19  LJDZ44 89.3715338         II
20  LJDZ51 63.1057644         II
21  LJDZ52 81.7604611         II
22  LJDZ54 13.1148849         II
23  LJDZ56 40.8284712         II
TarJae
  • 72,363
  • 6
  • 19
  • 66
1

IN base R, you could do use cut as shown below:

df$Hazard_Level <- cut(df$Risk_value, breaks =c(-Inf, 10, 100, 1000, Inf), 
                                      labels = c('I', 'II', 'III', 'IV')) 
df
   Station Risk_value Hazard_Level
1   LJDZ05 26.6273099           II
2   LJDZ06  1.0686316            I
3   LJDZ07  0.6720000            I
4   LJDZ08  3.4071579            I
5   LJDZ09 56.3451429           II
6   LJDZ11  0.5845714            I
7   LJDZ17  9.6218797            I
8   LJDZ18  2.3430354            I
9   LJDZ19  2.1771541            I
10  LJDZ20  2.2356090            I
11  LJDZ21  6.4386466            I
12  LJDZ23 14.2060602           II
13  LJDZ25 44.5303459           II
14  LJDZ36 22.4441461           II
15  LJDZ38 37.1921549           II
16  LJDZ39 33.5898413           II
17  LJDZ40 13.1131028           II
18  LJDZ42 12.4268271           II
19  LJDZ44 89.3715338           II
20  LJDZ51 63.1057644           II
21  LJDZ52 81.7604611           II
22  LJDZ54 13.1148849           II
23  LJDZ56 40.8284712           II

Another way would be:

df$Hazard_Level <- c('I', 'II', 'III', 'IV')[findInterval(df$Risk_value, c(-Inf, 10, 100, 1000, Inf))]
Onyambu
  • 67,392
  • 3
  • 24
  • 53
  • Dear great Onyambu. Would you please spent 1 minute of your valuable time for critical review of my answer here . Thank you! – TarJae Aug 27 '21 at 17:33
  • Didi your code. "Error in `$<-.data.frame`(`*tmp*`, Hazard_Level, value = character(0)) : replacement has 0 rows, data has 24" – Kazi Aug 27 '21 at 17:37
  • @Kazi There should be no errors. Just try the code exactly the way its written. NOte that I am not creating an empty vector before hand. Everything is done directly to the dataframe – Onyambu Aug 27 '21 at 17:46
  • Thanks Brother, but still I need to do in base statement, can you please help? – Kazi Aug 27 '21 at 17:57
  • @Kazi what exactly do you mean? – Onyambu Aug 27 '21 at 18:01
0

Yes you can use ifelse() function from from base r like this: Remeber Loading magrittr or dplyr for the pipe"%>%"

library(dplyr)

new_df <- df %>% mutate(Hazard_Level = ifelse(Risk_value  < 10,
                                    yes = "I",
                                    no = ifelse(Risk_value >= 10 & Risk_value < 100,
                                           yes = "II",
                                           no = ifelse(Risk_value >= 100 & Risk_value < 1000,
                                                       yes = "III",
                                                       no = "IV"))))
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Still i need to know the base statement and what is wrong with my code. Can you please help. – Kazi Aug 27 '21 at 17:36
  • Error can be it´s **else if** not **elseif** and Hazard_Level needs to be like **Hazard_Level[i]** don´t use append, just **Hazard_Level[i] <- "I"** and so on. Then df$Hazard_Level <- Hazard_Level – Mariano Stone Aug 27 '21 at 18:02