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.
There is a fre
function in the expss
package:
library(expss)
data(mtcars)
mtcars = apply_labels(mtcars,
mpg = "Miles/(US) gallon",
cyl = "Number of cylinders",
disp = "Displacement (cu.in.)",
hp = "Gross horsepower",
drat = "Rear axle ratio",
wt = "Weight (lb/1000)",
qsec = "1/4 mile time",
vs = "Engine",
vs = c("V-engine" = 0,
"Straight engine" = 1),
am = "Transmission",
am = c("Automatic" = 0,
"Manual"=1),
gear = "Number of forward gears",
carb = "Number of carburetors"
)
# as.list is needed to process data.frame as several variables rather than multiple response
calculate(mtcars, fre(as.list(vs %to% carb)))
Generally speaking, you can use %to%
inside calculate
with any other function from any package. %to%
simply returns data.frame, e.g vs %to% carb
is identical to mtcars[, c("vs", "am", "gear", "carb")]
.
Example with sjPlot:
library(sjPlot)
calc(mtcars, view_df(vs %to% carb))