I'm trying to run a set of frequency tables in R without having to write the code for every single variable. For example, using the mtcars data in SPSS I would so something like:
FREQUENCIES mpg TO vs
And it would give me the 8 frequency tables for the variables between mpg and vs. I'm trying to get this effect in R using the summarytools
function freq
or the sjPlot
function view_df
. I can do it using freq
but you have to list the names of all of the variables instead of using a command like TO
. And I can do it using view_df
but you have to know the column positions of the variables (I have thousands of variables so that's not going to work). Please take a look at what I've got below.
#####USING FREQ IN SUMMARY TOOLS
library(summarytools)
freq(mtcars[ ,c("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs")]) #works fine, but I don't want to have to list the names of all of the variables
#####USING VIEW_DF IN SJPLOT
library(sjPlot)
view_df(mtcars[, c(1:8)], #I want to be able to say c(mpg:vs)
show.na = TRUE,
show.type = TRUE,
show.frq = TRUE,
show.prc = TRUE,
show.string.values = TRUE,
show.id = TRUE)
####A FEW EXTRA STEPS USING THE EXPSS PACKAGE
I know you can use the %to%
in the expss
package. I've got my own data and variable names here, sorry!
# table with counts
counts = calculate(olbm_na_A, cro(mdset(S06_01_NA %to% S06_99_NA), list("Count")))
# table with percents
percents = calculate(olbm_na_A, cro_cpct(mdset(S06_01_NA %to% S06_99_NA), list("Column, %")))
# combine tables
expss_output_viewer()
(counts %merge% percents)
I expect to have it print out a sequence of frequency tables. I want to be able to use some command that basically means var1 to var10. I can't figure out how do this TO
command. I expect it varies by what package you're using.