A Recaps in 1 minutes or less
A.1 Possibilities of using R
Console: type a command and hit Enter
- Base R Console
- RGui Console on Windows
- RStudio Console
Script: edit a text files and hit Ctrl+R or Ctrl+Enter
- RGui Script Window (Ctrl+R)
- RStudio Source Pane (Ctrl+Enter)
Point and Click
- R Commander
- jamovi, JASP etc.
A.2 Console features
- history of command: Up/Down arrows
- autocompletion: TAB
- continuation prompt: Esc
A.3 Advantages of Script editor in RStudio
- multi-line editor
- full-featured text editor: e.g. row numbering, syntax highlighting
- autocompletion of filenames, function names, arguments and objects
- cross-platform interface to R
- surrounded by integrated graphical environment (workspace, files, plots, help, etc.)
A.4 Useful keyboard shortcuts in RStudio
- Ctrl+Enter: Run commands
- Clipboard Operations (Cut, Copy, Paste Operations): Ctrl+X, Ctrl+C, Ctrl+V
- Ctrl++, Ctrl+-: Zoom in/out
- Ctrl+Shift+C: Comment lines/uncomment lines
- Ctrl+F: Find and replace text within script editor
- Ctrl+S: Save the script file
- Alt+-: Write assignment operator
- Ctrl+Shift+F10: Restart R session
A.5 Base types in R
- character (or string):
"apple juice"
- integer (whole numbers):
12L
- double (real numbers, decimal numbers):
12
,12.4
- logical (true false type things):
TRUE
,FALSE
A.6 Data structures
- Vector: one-dimensional, homogeneous
- Matrix: two-dimensional, homogeneous
- Array: two or more dimensional, homogeneous
- List: one-dimensional, heterogeneous
- Factor: integer vector with levels, which is a character vector
- Data frame: two-dimensional, heterogeneous
Dimension | Homogenous | Heterogeneous |
---|---|---|
1D | Vector, Factor | List |
2D | Matrix | Data frame |
nD | Array |
A.7 Operators
Operator | Description | Example |
---|---|---|
:: |
access | MASS::survey |
$ |
component | my.s$Sex |
[ [[ |
indexing | my.s$Height[c(2, 45)] |
^ ** |
exponentiation | 2^3 |
- + |
unary minus, unary plus | -2 |
: |
sequence operator | 1:10 |
%any% e.g. %% %/% %in% |
special operators | 12%%3 |
* / |
multiplication, division | 12*3 |
+ - |
addition, subtraction | 2.3 + 2 |
< > <= >= == != |
comparisions | 2<=3 |
! |
logical NOT | !TRUE |
& |
logical AND | TRUE & FALSE |
| |
logical OR | TRUE | FALSE |
<- |
assignment | col <- 12 |
R language provides following types of operators:
- Arithmetic Operators:
^
**
,-
(unary),+
(unary),%%
,%/%
,*
,/
,-
(binary),+
(binary) - Relational Operators:
<
,>
,<=
,>=
,==
,!=
,%in%
- Logical Operators:
!
,&
,|
- Assignment Operators:
<-
- Miscellaneous Operators:
::
,$
,[
,[[
,:
,?
A.8 Maths functions
Function | Description | Example |
---|---|---|
abs(x) | Takes the absolute value of x | abs(-1) |
sign(x) | The signs of x |
sign(pi) |
sqrt(x) | Returns the square root of x | sqrt(9+16) |
exp(x) | Returns the exponential of x | exp(1) |
log(x,base=exp(1)) | Takes the logarithm of x with base y; if base is not specified, returns the natural logarithm | log(exp(3));log(8,10) |
log10(x);log2(x) | Takes the logarithm of x with base 10 or 2 | log10(1000);log2(256) |
cos(x);sin(x);tan(x) | Trigonometric functions | cos(pi);sin(0);tan(0) |
round(x,digits=0) | Rounds a numeric input to a specified number of decimal places | round(c(1.5,-1.5)) |
floor(x) | Rounds a numeric input down to the next lower integer | floor(c(1.5,-1.5)) |
ceiling(x) | Rounds a numeric input up to the next higher integer | ceiling(c(1.5,-1.5)) |
trunc(x) | Truncates (i.e. cuts off) the decimal places of a numeric input | trunc(c(1.5,-1.5)) |
A.9 String functions
Function | Description | Example |
---|---|---|
paste();paste0(sep="") |
Concatenate strings | paste('a','b',sep='=') |
nchar(x) |
Count the number of characters | nchar('alma') |
substr(x,start,stop) |
Substrings of a character vector | substr('alma', 3, 5) |
tolower(x) |
Convert to lower-case | tolower('Kiss Géza') |
toupper(x) |
Convert to upper-case | toupper('Kiss Géza') |
chartr(old,new,x) |
Translates characters | chartr('it','ál','titik') |
cat(sep=" ") |
Concatenate and print | cat('alma','fa\n',sep='') |
grep();grepl();regexpr() |
Pattern matching | grepl(pattern='lm',x='alma') |
sub();gsub() |
Pattern matching and replacement | gsub('lm',repl='nyj',x='alma') |
A.10 Base R Statistical Functions
Function | Description | Example | Value of Example |
---|---|---|---|
max(x) | The largest value of x |
max(1:10) | 10 |
min(x) | The smallest value of x |
min(11:20) | 11 |
sum(x) | The sum of all the values of x |
sum(1:5) | 15 |
prod(x) | The product of all the values of x |
prod(1:5) | 120 |
mean(x) | Mean of x |
mean(1:10) | 5.5 |
median(x) | Median of x |
median(1:10) | 5.5 |
range(x) | The minimum and the maximum | range(1:10) | 1 10 |
sd(x) | Standard deviation of x | sd(1:10) | 3.03 |
var(x) | Variance of x |
var(1:10) | 9.17 |
cor(x,y) | Correlation between x and y |
cor(1:10,11:20) | 1 |
A.11 Regular sequences
Function | Description | Example | Value of Example |
---|---|---|---|
from:to |
generates a sequence from from= to to= in steps of 1 or -1 |
1:5 |
1 2 3 4 5 |
seq(from, to, by, length.out) |
generate regular sequences | seq(from=2, to=10, by=2) |
2 4 6 8 10 |
rep(x, times, each) |
replicate elements of vectors | rep(x=0, times=3) |
0 0 0 |
paste(sep, collapse) |
concatenate vectors | paste("No", 1:3, sep="_") |
"No_1" "No_2" "No_3" |
A.12 Subsetting
Data structure | Example |
---|---|
|
|
|
|
|
|
|
|
A.13 Packages
Operation | Example |
---|---|
Install a package from CRAN | install.packages("package_name") |
Load a package | library(package_name) |
A.14 Reading/Writing data files
Operation | Example |
---|---|
Import text files | read.table(file, sep, dec, header, fileEncoding) |
Import Excel or SPSS files | rio::import(file) |
Export text files | write.table(x, file, sep, dec, row.names, quote, fileEncoding) |
Export Excel or SPSS files | rio::export(x, file) |
A.15 Filter
Data structure | Example |
---|---|
|
|
|
|
A.16 Sort
Data structure | Example |
---|---|
|
|
|
|
|
|
A.17 Data type conversion
Conversion | Example |
---|---|
|
|
|
|
|
|
|
|
A.18 Transformation
Transformation | Example |
---|---|
|
|
|
|
|
|
A.19 Descriptive statistics
Descriptive statistics | Example |
---|---|
|
|
|
|
|
|