Lab 00: Install R and R Studio
Students should solve exercises provided in the in-class exercise session on every Friday.
Students should write (a single) R code about answers of exercises, submit it on course website.
Please check whether your answers are uploaded well.
We will not give any scores for all late submission. Please keep the time.
You may leave early after submitting your answers on the course website.
Introduction
Welcome to the world of statistical programming!
Install R and RStudio
R can be downloaded from
https://cloud.r-project.org
. Most users should download and install a binary version.R is designed to be very portable: it will run on Microsoft Windows, Linux, Solaris, macOS, and other operating systems.
You should also install RStudio, after you have installed R, from
https://posit.co/download/rstudio-desktop/
.
Getting started in RStudio
- When you are working in RStudio, you’ll see a display something like
- The display includes four panes.
- The top left pane is the Source Pane, or editor. You will type your program (or other document) there. You can have several open files in this pane; the tabs allow you to choose which is active.
- The bottom left pane is called the Console Pane. This is where you communicate with R. You can type directly into this pane, but it is usually better to work within the editor pane, because that way you can easily correct mistakes and try again.
- The two right-hand panes contain a variety of tabs. In the figure, the top pane is showing the Environment Pane (i.e. the workspace), and the bottom pane is showing a plot.
- For now, you just need to know the following points:
- You should do most of your work in the editor, but you can occasionally type in the console.
- The console pane displays what R is doing.
- All of the panes can be resized and repositioned, so sometimes it may appear that you’ve lost one, but there’s no need to worry: just find the header of the pane and click there with your mouse, and the pane will reappear. If the pane is there but the content isn’t what you want, try clicking on the tabs at the top.
Exercises
Basic R exercise
Here’s a simple exercise with an empty code chunk provided for entering the answer.
1. Write the R code required to add two plus two:
Exercise with R function
2. Now write an R function add
that adds any two numbers and then call it:
Assignment
You can use <-
or =
to assign values to a specific variable:
3. Now write an R variable studentinfo
which contains your student ID and name:
Number of rows
4. Modify the following code to limit the number of rows printed to 5: