rfrequencymissing-data

How to include NA frequencies when making a list of frequency tables?


I have the dataframes df, df1, and the list l1 as follows:

df = data.frame(x = c(1,0,0,0,1,1,1,NA), y = c(2,2,2,2,3,3,2,NA),
                z = c(1:7,NA), m = c(1,2,3,1,2,3,1,NA) )

df$x = factor(df$x)
df$y = factor(df$y)
df$m = factor(df$m)

df1 = df%>%select_if(is.factor)

l1 = lapply(df1,table(useNA = 'always'))

when trying to include the useNA argument in l1 list, it doesn't work. However, when applying the argument for each variable like for example table(df1$x,useNA = 'always'), it works properly.


Solution

  • The proper format for lapply is "lapply(X, FUN, ...)" where the ... are for the optional arguments for the function.

    So in this case the argument: useNA = 'always' becomes:

    lapply(df1, table, useNA = 'always')