R Basics
R as a calculator
First steps we can do is to use R as a calculator. Type the code directly in the Console pane where > is shown, and press Enter to run (execute) it. The > sign, is promt, that R is ready to accept commands.
The result will be printed below the command:
The [1] is just console line label, which is useful when printing objects containing more values, which will be explained later.
So you should see this in your console:
Use ** or ^ for exponentiation
Relational operators
You can also compare values to test whether they meet a condition. This returns a logical value, TRUE or FALSE.
==- equals!=- not equals>- greater than<- less than>=- greater or equal<=- less or equal
try:
Variables
Values and other objects can be assigned to variables with operator <-. Executing the variable name is call printing and will print the value assigned in it. Imagine variables as links to the stored values.
Assigning a value to existing variable will overwrite the previous value.
The result of any operation can be assigned to a variable:
Variables names:
- can't contain spaces and special characters
- can't start with number
- are case sensitive
- can't be some reserved words (
if,for, ...)
- snake_case - used in this course
- camelCase
- dot.case - not recommended
Use what you like, but be consistent.
Text values
We will also work with text values, which are written in quotes, either single 'text' or double "text".
Functions
Calling function consist of function name and usually arguments in parentheses. Arguments can be named or unnamed.
function_name(argument1 = value1 ,argument2 = value2, argument3 = value3, ...). There are also functions that doesn't have any arguments (getwd()).
Try functions round() and abs():
You can get function help using RStudio GUI or calling ? before the function name, or help() function. Help gives you information about the function, its arguments and their default values, and usage examples.
In help we found the argument digits, try it:
or just
Note
Actually the function runs
wherex is the first unnamed argument.
Another simple function is abs() - absolute value:
Writing scripts
As mentioned in the Introduction to R there are various ways to write and run code in R, interacively in the console, or writing scripts that can be saved and run later (entire or by parts). Most of the time you will write code to scripts (.R or .r files). Code can be written in any text editor.
Runing code
In RStudio you can write and save scripts in the Source pane. You can run the entire script or just the selected part of the code.
- Run selected part of the code -
Ctrl + Enter- or buttonRun- this you willl use mostly in this course. You can run the code line by line, or by selected blocks. This function sends the code to the console and run it, and the cursor will move to the next line or executable part of code.
Comments
In writing scripts you can "comment" lines. Commented lines starts with # and are not executed (even if they are sent to the console). Coments are useful for documenting/explanation code or "deactivating" parts of code.
Summary
Main outcomes
- use R as calculator and understand basic value comparisons interactively in console
- what is variable and how to assign values to it with
<- - how to use functions and get help for them
- how to write and run code from scripts (source pane in RStudio)
Function overview
round()- round a number to a specified number of decimal placesabs()- absolute value of a number