Skip main navigation

Using RStudio: Frequency tables, lists, and exporting graphics

How to use RStudio to create frequency tables, lists, and exporte graphics.

In the previous step we looked at how to get started with RStudio and import your dataset. In this step we’ll look at some basic commands you can run and where to go to find out more information. 

Frequency tables and lists

Given some categorical data gathered as a list of observations (such as the GSS dataset considered above), it may be helpful to produce a frequency table, with observed counts of different values.

To this end, we can use the command  table(),  for example:

gss_tab <- table(gss); print(gss_tab) 
## tvhours 
## 0 1 2 3 4 5 6 7 8 9 10 12 13 14 15 16 18 20 22 24 
## 90 255 325 238 171 61 58 19 31 3 17 11 2 4 3 1 1 2 1 5 

It is easy to modify this code to get a table of relative frequencies (or proportions):

gss_tabprop <- round(gss_tab / sum(gss_tab), 3) 
print(gss_tabprop) 
## tvhours 
## 0 1 2 3 4 5 6 7 8 9 10 12 13 
## 0.069 0.196 0.250 0.183 0.132 0.047 0.045 0.015 0.024 0.002 0.013 0.008 0.002 
## 14 15 16 18 20 22 24 
## 0.003 0.002 0.001 0.001 0.002 0.001 0.004 

Conversely, data represented as a frequency table (such as the women’s clothing size dataset considered above) may need to be converted to a list using the replication command  rep():

f1 <- rep(f$size, f$freq); head(f1, 17) 
# show first 17 values 
## 6 8 8 8 8 8 8 8 8 8 8 8 10 10 10 10 10 

Exporting graphics

Plots created in the RStudio session can be navigated by using the left and right arrow buttons in the toolbar of the Graphics pane.

Any undesired plot can be deleted by pressing the Remove button; it looks like a white cross sign in a red circle.

You can save a plot by choosing the Export icon in the Graphics toolbar and selecting one of the options in the opening menu: Save as PDF or Save as Image (further selecting a suitable format, such as PNG or EPS).

Using the help system

RStudio enjoys a handy help system. This can be accessed by clicking Help > R Help in the top menu which opens up a variety of further options, such as ‘Learning R Online’ under R Resources or ‘Posit Support’ and ‘RStudio Cheat Sheets’ under RStudio.

A more systematic exposition of R is available as a searchable HTML document ‘An Introduction to R’ under Manuals; it can also be accessed directly by clicking Help > Search R Help.

Most of the help pages have examples (at the end) which are extremely useful for understanding how a command works.

They may deploy some standard datasets stored within R.

You can copy and paste these examples into your Editor pane and execute them in RStudio to gain further understanding.

If you know (or can guess) the name of a command (e.g  mean) and wish to get help or comments about it, type and execute  help(mean)  or  ?mean  – this will open a web page in your web browser with the required information.

To see and run just the examples, use the command example():

example(mean) 
## mean> x <- c(0:10, 50) 
## mean> xm <- mean(x) 
## mean> c(xm, mean(x, trim = 0.10)) 
## 8.75 5.50 

Next steps

After reading these useful tips, you should be ready to try out R commands in RStudio. In the next reading, you learn the R code for graphical and numerical summaries.

This article is from the free online

Statistical Methods

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now