dbosak01 / fmtr

An R package for formatting data.
12 stars 1 forks source link

Apply 1 format from a catalog to multiple columns #72

Closed sbrown-iu closed 2 months ago

sbrown-iu commented 2 months ago

Thanks so much for the great packages, just discovered the sassy packages and they are helping quite a bit. I have some data where the same format is applicable to dozens of columns, and I am struggling to find an efficient way to use fmtr functions to apply them as needed. Ideally I want to create formats in a catalog, then apply each to multiple columns of a data frame. Any guidance would be appreciated. Thanks!

Below is what I currently am doing. But I may have 50 columns with the same format and do not want to have to create a separate format value for each column, if possible.

fmts <- fcat( col1=value(condition(x==1, 'Better or no change'), condition(x==2, 'Questionable/occasionally worse'), condition(x==3, 'Consistently a little worse'), condition(x==4, 'Consistently much worse'), condition(x==5, 'Unknown'), condition(x==9, 'Do not know'))

col2=value(condition(x==1, 'Better or no change'), condition(x==2, 'Questionable/occasionally worse'), condition(x==3, 'Consistently a little worse'), condition(x==4, 'Consistently much worse'), condition(x==5, 'Unknown'), condition(x==9, 'Do not know')) )

formats(df) <- fmts df<- fdata(df)

dbosak01 commented 2 months ago

Hi Steve: You can create the survey format before creating the catalog, put it in an independent variable, then reference that format in several locations in the format catalog. Something like this:

# Create format
surv1=value(condition(x==1, 'Better or no change'),
condition(x==2, 'Questionable/occasionally worse'),
condition(x==3, 'Consistently a little worse'),
condition(x==4, 'Consistently much worse'),
condition(x==5, 'Unknown'),
condition(x==9, 'Do not know'))

# Reference in multiple places
fmts <- fcat(
col1 = surv1,
col2 = surv1
)

formats(df) <- fmts
df<- fdata(df)

You can also assign formats to columns directly instead of assigning to a format catalog. Basically, you just use list() instead of fcat(), and everything else is the same.

For me, I would probably put each unique format in a format catalog, and then take them from the format catalog and assign to the list:

# Create format catalog with unique formats
fc <- fcat(
surv1=value(condition(x==1, 'Better or no change'),
condition(x==2, 'Questionable/occasionally worse'),
condition(x==3, 'Consistently a little worse'),
condition(x==4, 'Consistently much worse'),
condition(x==5, 'Unknown'),
condition(x==9, 'Do not know')),
surv2 = [whatever])

# Reference in multiple places
fmts <- list(
col1 = fc$surv1,
col2 = fc$surv1
col3 = fc$surv2
)

formats(df) <- fmts
df<- fdata(df)

That way you can save the formats to disc and use them in another program, without having to redo everything in the other program. It is the same way you would do it in SAS.

Hope this helps!

sbrown-iu commented 2 months ago

Ah, this is great! Yes the second solution is what I was looking for, was just not understanding the list option. But now I do. Thanks so much for the quick reply. I am spreading the gospel to colleagues about the fmtr package, so great to be able to apply formats in a way similar to SAS. Thanks again!