Fixed specification error: the function fails when condition is a factor.
Added a line to convert condition into a character vector.