Variable formats must be of class character and changed many sapply to vapply