R crosstab with thousands delimiter

Hi all,

I am trying to create a cross tab table that is weighted. The counts appear like this 100000 and I would like them to appear like this 100,000. I also would like to export to a jpg. Any ideas on how to do that? While i'm using sjpot, i'm open to anything that will do the job and provides both counts and percentages.

Thanks,

Dan

library(sjPlot)

sjt.xtab(KFF2017$b20a,KFF2017$size7, weight.by =KFF2017$empwt,show.col.prc = TRUE)

Try:

format(x, big.mark = ",")

Thanks I tried that. It doesn't work for xtab nor for crosstab any other ideas

TR<-xtabs(KFF2017$empwt~ KFF2017$b20a+KFF2017$size7)
pander(format(TR,big.mark=','))
gets me close but i need to show counts and percentages

Just use sprint() or glue::glue() to form your own formatting function.

Hi @dtlbyrd, it would help us a lot if you could produce a more complete reprex, or reproducible example, that includes a bit of sample data. The context helps us see which data is going into the crosstab so that we can pinpoint what's going wrong :slight_smile:

1 Like

Hi all,

Sorry about that. I'm a former SAS and Stata user, and have no experience with R. The reprex doesn't work with my version of R.

Goal: My goal is to create a table like this that has commas for thousands delimiters. This example doesn't have that.

table

Problem: I'm using sjplot (to create the above plot), and I have also tried xtable and crosstab. All of the results have been the same, no comma delimiters for numbers. I am working with weighted survey data.

Does anyone know how to modify sjplot to produce a chart where numbers have thousands delimiters or know another package that can accomplish this task

This is the R code I was using

sjt.xtab(KFF2017$b20a,KFF2017$size7, weight.by =KFF2017$empwt,show.col.prc = TRUE)

I hope this helps.

Thanks again!

Dan

No worries, @dtlbyrd :slight_smile: I'm mostly looking to understand what columns are in your data frame (KFF2017) and what some of the values look like.

I'm not super familiar with how the cross tab table works, but I'd suggest creating the pre-formatted values as separate columns and then using those in your table. The scales package, which is used by ggplot2, comes with a heap of really easy to use formatting functions for numbers, percentages, currencies, etc.. I'd recommend using those first, and then pass the formatted columns in. That way you don't need to modify sjPlot; you just need to prep the data yourself :slight_smile:

General question - general answer :smile: No reprex - no code :smiley:
But we can try to solve it anyway.

Conceptually, you have a cross-tab, and you need the values in the rows and columns to be formatted the way you want (with a coma), right?

I'd take a few steps: Wrange the data to get all values of the same type into one column (dplyr::gather()), then style it the way I want it (scales package), and then spread() back. Conceptually, that's what I'd try to do if this was my task.

Without a reprex I'm not sure if anyone can help you any further than providing some general ideas.
Hope this helps.

Thank you for your help! I will try that

Thank you!. This is a great start

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.