Creating new column with strings based on values in a column

So I am trying to create a temperature gradient by colors based on temperature values. I have the following example:

> dput(head(temp_data))
structure(list(Date = structure(c(726624000, 726710400, 726796800, 
726883200, 726969600, 727056000), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), Row = c(1, 2, 3, 4, 5, 6), High = c(29, 22, 29, 35, 
33, 33), Low = c(16, 18, 20, 25, 13, 17), Average = c(22.5, 20, 
24.5, 30, 23, 25)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

This provides me with a table of dates and temperature values. I have ten different quantile values:

> quantile(temp_data$Average,prob=c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9),type=1)
 10%  20%  30%  40%  50%  60%  70%  80%  90% 
24.0 31.0 36.5 40.5 48.5 54.0 61.0 68.0 74.0 

I want to create a column that pastes a string color name based on the temperature value. For example, if the average is less than 24.0 then I want the value in the new column to be "navy", and I want to have a different color for each of the quartile ranges. Is there a good way to write this? Do I need a for loop?

I think you can use the cut() function setting the colors in the labels argument. Here is a toy example with fewer cut points than your example.

temp_data <- structure(list(Date = structure(c(726624000, 726710400, 726796800, 
                                  726883200, 726969600, 727056000), tzone = "UTC", class = c("POSIXct", 
                                                                                             "POSIXt")), Row = c(1, 2, 3, 4, 5, 6), High = c(29, 22, 29, 35, 
                                                                                                                                             33, 33), Low = c(16, 18, 20, 25, 13, 17), Average = c(22.5, 20, 
                                                                                                                                                                                                   24.5, 30, 23, 25)), row.names = c(NA, -6L), class = c("tbl_df", 
                                                                                                                                                                                                                                                         "tbl", "data.frame"))
cutpoints <- quantile(temp_data$Average,prob=c(0.1,0.3,0.5,0.7,0.9),type=1)
temp_data$Color <- cut(temp_data$Average,breaks = c(-100, cutpoints, 100), 
    labels = c("color1", "color2","color3","color4","color5","color6"))
temp_data
#>         Date Row High Low Average  Color
#> 1 1993-01-10   1   29  16    22.5 color2
#> 2 1993-01-11   2   22  18    20.0 color1
#> 3 1993-01-12   3   29  20    24.5 color4
#> 4 1993-01-13   4   35  25    30.0 color5
#> 5 1993-01-14   5   33  13    23.0 color3
#> 6 1993-01-15   6   33  17    25.0 color4

Created on 2023-05-25 with reprex v2.0.2

Are you doing this for a plot? There are probably easier ways to map quantities or categories to colors.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.