0

I want to add a column “site” to my data frame, where a site name “ISF”,”CB” etc. is assigned to all datapoint falling into the specified Lat/Lon coordinates (they are rectangles). The code runs, but the newly created column stays empty. Whats wrong?

testing %>% mutate(site =
                     case_when(Lon>=-4.5 & Lon<=-6 & Lat>=53.83333 & Lat<=54.166667 ~ "ISF")),
                 Lon>=-4 & Lon<=-5 & Lat>=53.166667 & Lat<=53.666667 ~ "NorthAng",
                 Lon>=-4 & Lon<=-5 & Lat>=52 & Lat<=53 ~ "CB", 
                 Lon>=-5 & Lon<=-6 & Lat>=51.5 & Lat<=52 ~ "CD”))
Paul
  • 2,850
  • 1
  • 12
  • 37
  • 3
    Because you longitude operators are the wrong way round. Nothing is greater than -4.5 but less than -6. – Quixotic22 Aug 10 '21 at 08:17
  • They are also some typos in your code, like wrong quotation marks: `"CD”` sould be `"CD"` and wrong parenthesis. Try with `mutate(site = case_when( Lon>=-4.5 & Lon<=-6 & Lat>=53.83333 & Lat<=54.166667 ~ "ISF", Lon>=-4 & Lon<=-5 & Lat>=53.166667 & Lat<=53.666667 ~ "NorthAng", Lon>=-4 & Lon<=-5 & Lat>=52 & Lat<=53 ~ "CB", Lon>=-5 & Lon<=-6 & Lat>=51.5 & Lat<=52 ~ "CD" )`. Moreover, it is difficult to help you without a sample of your data. Please see https://stackoverflow.com/q/5963269/10264278 – Paul Aug 10 '21 at 13:07

0 Answers0