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:
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():
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.