converting sql to width bins in r I want to create a histogram out of the range of a column named surfacearea with country data frame. I made an sql file 

# In R
range &lt;- max(country$surface) - min(country$surfacearea)

#in SQL
# part B Divide the range into 5 equal width bins
#SELECT CASE 
#WHEN surfacearea  &lt;  3415080 THEN "LOW"
#WHEN surfacearea &lt;= 6830160 and surfacearea &gt; 3415081 THEN "MEDIUM-LOW"
#WHEN surfacearea &lt;= 10245240 and surfacearea &gt; 6830161 THEN "MEDIUM"
#WHEN surfacearea &lt;= 13660320 and surfacearea &gt; 10245240 THEN "MEDIUM-HIGH"
#ELSE "HIGH" END AS `type`, COUNT(surfacearea)
#GROUP BY `type`
#ORDER BY `type` ASC;


I'm working on putting mySQL into r
You could use the dplyr package and its case_when function. Note the spacing is just for readability:

library(dplyr)

country %&gt;%
  mutate(
    type = case_when(
      surfacearea &lt;  3415080                           ~ "LOW",
      surfacearea &gt;  3415081 &amp; surfacearea &lt;=  6830160 ~ "MEDIUM-LOW",
      surfacearea &gt;  6830160 &amp; surfacearea &lt;= 10245240 ~ "MEDIUM",
      surfacearea &gt; 10245240 &amp; surfacearea &lt;= 13660320 ~ "MEDIUM-HIGH",
      TRUE                                             ~ "HIGH"
    )
  )


The last line is the catch-all case. For more see ?case_when. To complete your pipeline, you could do:

country %&gt;%
  group_by(type) %&gt;%
  mutate(
    type = case_when(
      surfacearea &lt;  3415080                           ~ "LOW",
      surfacearea &gt;  3415081 &amp; surfacearea &lt;=  6830160 ~ "MEDIUM-LOW",
      surfacearea &gt;  6830160 &amp; surfacearea &lt;= 10245240 ~ "MEDIUM",
      surfacearea &gt; 10245240 &amp; surfacearea &lt;= 13660320 ~ "MEDIUM-HIGH",
      TRUE                                             ~ "HIGH"
    )
  ) %&gt;%
  arrange(type)


There is also the between function which could further help the readability, but that function is a shortcut for x &gt;= left &amp; x &lt;= right so it might not work since your first bucket is strictly less-than.