Tables in R and sjPlot

Building tables in R is a simple process that is also extremely flexible. Using the NHANES data set as a further example, we can build a table out of two variables that we previously created:

> table(age.category,BMI.category)
             BMI.category
age.category underweight normal overweight obese
       child        1537    519        117    47
    teenager         111    390        143   113
       adult          50    765        630   660
      mature          31    421        638   703
      senior          30    494        732   730

Or, using sjPlot, we can clean this data up, maybe get it ready for publication:

> install.packages("sjPlot")
> sjt.xtab(age.category,BMI.category, variableLabels = 
c("Age", "BMI Frequency"), showRowPerc=TRUE, showColPerc=TRUE)

 

screen-shot-2017-01-28-at-9-18-56-pm

We have not controlled for gender in partitioning this data, something that a few key strokes will allow us to do:

> bmi.age.gender = table(BMI = BMI.category, 
                         age = age.category, gender)
> bmi.age.gender

, , gender = Male
             age
BMI          child teenager adult mature senior
 underweight   818       67    14     10     15
 normal        259      199   393    184    227
 overweight     52       75   354    374    390
 obese          23       53   291    313    337

, , gender = Female
             age
BMI          child teenager adult mature senior
 underweight   719       44    36     21     15
 normal        260      191   372    237    267
 overweight     65       68   276    264    342
 obese          24       60   369    390    393

It is quick and easy to separate values based on gender, and export that product as a PDF, or JPEG image.

> sjt.xtab(BMI.category,age.category,gender, 
              variableLabels = c("Age", "BMI", "Gender"), 
          showHorizontalLine = TRUE, 
                 showRowPerc = TRUE, 
                 showColPerc = TRUE)

genderbmi

Advertisements

About dwmaasberg

Memories are physical connections between neurons. I think that is pretty cool!
This entry was posted in R, Statistics. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s