Formatting data to display meaningful titles instead of existing numbers.

I am trying to apply formatting to my data so that numerical outputs have meaningful labels when queried. I have installed and applied the fmtr package (which doesn't show in the list for tagging this post). In the below sample I create a variable similar to my dataset with age is coded as four categories (1,2,3,4). 1 means the person is between the aged of 0-17, 2 means 18-44, 3 means 45-64, and 4 means 65 or older.

Normally, when printing a table using table() it prints:

age4cat
1 2 3 4 
9 8 9 5

What I WANT is:

Age 0-17 years      Age 18-44 years     Age 45-64 years      Age 65 or more years
9                    8                   9                    5

The hold up seems to be with fapply(), which kills the code and give the following error:

Error in if (!is.na(tmp) & !is.null(tmp)) { :
the condition has length > 1

Here is my code along with the sample data I prepared for testing:

#This is just sample data. My actual data has over 200,000 observations
age4cat <- c(1,1,1,2,3,2,4,1,2,3,1,4,1,2,3,4,3,3,2,1,2,3,4,3,2,1,1,3,2,3,4)

#This is the actual coding that I am working with
age4cat_fmt <- value(condition(age4cat == 1, "Age 0-17 years"),
                     condition(age4cat == 2, "Age 18-44 years"),
                     condition(age4cat == 3, "Age 45-64 years"),
                     condition(age4cat == 4, "Age 65 or more years"),
                     condition(age4cat == 5, "Coding error"))

fapply(age4cat, age4cat_fmt)

table(age4cat)

Part of the problem is using age4cat as the argument to the condition function. It should be x. Also, the result of fapply() needs to be fed to table(), rather than feeding it age4cat. Try the following:

library(fmtr)

#This is just sample data. My actual data has over 200,000 observations
age4cat <- c(1,1,1,2,3,2,4,1,2,3,1,4,1,2,3,4,3,3,2,1,2,3,4,3,2,1,1,3,2,3,4)

#This is the actual coding that I am working with
age4cat_fmt <- value(condition(x == 1, "Age 0-17 years"),
                     condition(x == 2, "Age 18-44 years"),
                     condition(x == 3, "Age 45-64 years"),
                     condition(x == 4, "Age 65 or more years"),
                     condition(x == 5, "Coding error"))

result <- fapply(age4cat, age4cat_fmt)

table(result)