tc <- function(x) {
  return (head(color.id(x), n = 1))
 csv_url = "https://raw.githubusercontent.com/rtanglao/2016-r-rtgram/master/JANUARY2016/january2016-ig-van-avgcolour-id-mf-month-day-daynum-unixtime-hour.csv"
    average_colour_ig_van_jan2016 = read_csv(csv_url)
    # the following line of code doesn't work because mutate just does the first value of the column
    # average_colour_ig_van_jan2016_with_colourname = mutate(average_colour_ig_van_jan2016, colourname = tc(colour))
   average_colour_ig_van_jan2016_with_colourname =  average_colour_ig_van_jan2016 %>% 
       rowwise() %>% 
       mutate(colourname = tc(colour))
  • %>% is like the unix pipe operator
  • rowwise() is required to repeat the function call on each row, without rowwise() you’ll just repeat it row 1 repeatedly i.e. get the same value each time, the value of tc applied to the first row !
  • mutate adds the colourname column by computing the colourname from the hexadecimal colour using the color.id function from the plotrix package
  • This RScript was invoked as follows:
cd JANUARY2016
Rscript ../part2-create-csv-ig-van-average-colour-jan2016.R
  • In Part 3, we’ll create a visualization using the colour names.

Leave a comment on github