If you did not want to bother specifying colors, and you wanted to remove colors automatically, you could accomplish that as well.
The colors()
function returns a list of colors that are used in the current palette. We can then perform a little code manipulation in conjunction with the gsub()
function that we just used to replace all of the specified colors from OnlineRetail$Description
with blanks.
We will also use the kable()
function, which is contained within the knitr
package, in order to produce simple HTML tables of the results:
# compute the length of the field before changes before <- sum(nchar(OnlineRetail$Description)) # get the unique colors returned from the colors function, and remove any digits found at the end of the string # get the unique colors col2 <- unique(gsub("[0-9]+", "", colors(TRUE))) #Now we will filter out any colors with a length > 7. This number is somewhat arbitrary but it is just done for illustration...