I have a df that uses numbers to refer to categorical data and a CSV that defines the categories (1=Smoker, 2=Non-Smoker, etc). In SAS, I was able to convert the format CSV into a format file and apply these formats to the variables:
data want;
set have;
formatted = put(varX,custFormat.);
run;
This would provide me with the output:
varX formatted
1 Smoker
2 Non-Smoker
3 Occasional Smoker
1 Smoker
Given that I have a csv with all the formats, I could bring this in and merge to my R df to have the formats in a new column:
print(have)
varX
1
2
3
1
print(format.file)
formatIndex group
1 Smoker
2 Non-Smoker
3 Occasional Smoker
11 Female
12 Male
13 Unknown
df.format <- merge(have, format.file, by.x = "varX", by.y = "formatIndex")
print(df.format)
varX group
1 Smoker
2 Non-Smoker
3 Occasional Smoker
1 Smoker
The issue with a join approach is I often want to apply the same formats for many columns (i.e. varX, varY, and varZ all use different formatIndex). Is there a similar method of applying formats to variables as SAS has?