Repeat the comparison of "User tables" in R

I use SPSS every day, but really try to learn R. The main thing that is holding me back is my need to easily create tables, banners and crosstabs for market research that I do. I like the Custom Tables option in SPSS, and I'm looking for advice on how to copy it from R.

I believe that R has a lot of advantages over SPSS, one of which is the ability to integrate with LaTeX for reproducible reports. SPSS is great for quick research (point and click), but leaves much to be desired when I take the results and pack them into an acceptable result for customers, etc. However, R is so powerful that I feel that I can do everything I can. I need this if I could make my banners / crosstabs the way I need them.

In short, what are my options for creating tables worthy of a report, similar to the ones I have below? I am copying the SPSS syntax command and the output for reference.

CTABLES /VLABELS VARIABLES=age educ paeduc maeduc speduc prestg80 happy DISPLAY=DEFAULT /TABLE age [MEAN F40.3, VALIDN COMMA40.0] + educ [MEAN F40.3, VALIDN COMMA40.0] + paeduc [MEAN F40.3, VALIDN COMMA40.0] + maeduc [MEAN F40.3, VALIDN COMMA40.0] + speduc [MEAN F40.3, VALIDN COMMA40.0] + prestg80 [MEAN F40.3, VALIDN COMMA40.0] BY happy /SLABELS POSITION=ROW /CATEGORIES VARIABLES=happy ORDER=A KEY=VALUE EMPTY=INCLUDE TOTAL=YES POSITION=AFTER MISSING=EXCLUDE /SIGTEST TYPE=CHISQUARE ALPHA=0.05 INCLUDEMRSETS=YES CATEGORIES=ALLVISIBLE /COMPARETEST TYPE=MEAN ALPHA=0.05 ADJUST=BONFERRONI ORIGIN=COLUMN INCLUDEMRSETS=YES CATEGORIES=ALLVISIBLE MEANSVARIANCE=ALLCATS MERGE=NO /COMPARETEST TYPE=PROP ALPHA=0.05 ADJUST=BONFERRONI ORIGIN=COLUMN INCLUDEMRSETS=YES CATEGORIES=ALLVISIBLE MERGE=NO. 

I attached a picture of how the result looks. I am particularly interested in the ability to have multiple variables in rows / columns, and I like the flexibility of nesting them if I need to. In the figure, I have several continuous variables cut out by a categorical variable in a column, and the resulting statistics are placed in rows. In addition, I also really like the quick comparisons feature of average column values, but data can quickly access them in R to conditionally generate a crosstab.

+6
r spss
source share
6 answers

Try to study the "table" function from the "tables" package. I think this may be helpful.

+2
source share

This is what is currently not so simple. You may have to combine several functions from several packages to get this output.

+1
source share

I just downloaded the psych package, and it's pretty good for creating tables for summary statistics, broken into variables. it is not formatted as well as stata says. I think you can output it to a text file and then format it the way you want.

+1
source share

There were several presentations on this topic in useR 2010, so in the near future you can see more packages trying to solve this problem.

+1
source share

See the xtable package for some table exporting to LaTeX and HTML. However, there may be other packages. It also looks promising. Have you heard of Sweave?

0
source share

I also had problems many times with an unusable output R ... The only solution I found was to write my own function and I am happy to share it with you here:

The following function returns for all factor variables in data.frame the frequency or percentage (calc = "perc") for each level of the variable variable "variable".
Most importantly, the output is a simple user-friendly data.frame. Thus, there is no problem to export the results to work with it in any way.

I understand that there are many opportunities for further improvements, i.e. add the ability to select the calculation of the number of rows and columns, etc. This is a work in progress but is on a mission.

 contitable <- function( survey_data, variable, calc="freq" ){ # Check which variables are not given as factor # and exlude them from the given data.frame survey_data_factor_test <- as.logical( sapply( Survey, FUN=is.factor) ) survey_data <- subset( survey_data, select=which( survey_data_factor_test ) ) # Inform the user about deleted variables # is that proper use of printing to console during a function call?? # for now it worksjust fine... flush.console() writeLines( paste( "\n ", sum( !survey_data_factor_test, na.rm=TRUE), "non-factor variable(s) were excluded\n" ) ) variable_levels <- levels(survey_data[ , variable ]) variable_levels_length <- length( variable_levels ) # Initializing the data.frame which will gather the results result <- data.frame( "Variable", "Levels", t(rep( 1, each=variable_levels_length ) ) ) result_column_names <- paste( variable, variable_levels, sep="." ) names(result) <- c("Variable", "Levels", result_column_names ) for(column in 1:length( names(survey_data) ) ){ column_levels_length <- length( levels( survey_data[ , column ] ) ) result_block <- as.data.frame( rep( names(survey_data)[column], each=column_levels_length ) ) result_block <- cbind( result_block, as.data.frame( levels( survey_data[,column] ) ) ) names(result_block) <- c( "Variable", "Levels" ) results <- table( survey_data[ , column ], survey_data[ , variable ] ) if( calc=="perc" ){ results <- apply( results, MARGIN=2, FUN=function(x){ x/sum(x) }) results <- round( results*100, 1 ) } results <- unclass(results) results <- as.data.frame( results ) names( results ) <- result_column_names rownames(results) <- NULL result_block <- cbind( result_block, results) result <- rbind( result, result_block ) } result <- result[-1,] return( result ) } 
0
source share

All Articles