vendredi 27 août 2021

How can I solve this if else statement and merge the data in to one one dataframe?

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)

Aucun commentaire:

Enregistrer un commentaire