R Programming 101 Part 1
R Programming 101 Part 1
https://www.r-project.org
First step
> 20 / 5
[1] 4
>5*4
[1] 20
> 64 - 57
[1] 7
>8^2
[1] 64
Value assignment
• The <- symbol is the assignment operator
> x <- 7
> print(x)
[1] 7
>x
[1] 7
> class(x)
[1] "numeric”
> class(y)
[1] ”integer”
Vectors
• Vectors are objects which contain multiple values of the same class (with
the exception of a list and dataframe)
>x
[1] 1.31 2.35 0.91 -1.06 -0.54 0.86 -0.15 1.19 -1.40 -0.60 -1.44 -1.70 2.02
> class(x)
[1] "numeric"
Vectors
>y
[1] TRUE TRUE TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE
[12] FALSE TRUE FALSE TRUE TRUE FALSE FALSE TRUE FALSE
> class(y)
[1] ”logical"
Creating Vectors
• Use the c() function to create vectors (combine values)
> mat
[,1] [,2] [,3] [,4]
[1,] 1.31 0.86 -1.44 0.23
[2,] 2.35 -0.15 -1.70 -0.61
[3,] 0.91 1.19 2.02 -2.78
[4,] -1.06 -1.40 -0.50 1.41
[5,] -0.54 -0.60 1.72 -1.57
Matrices
• Use the dim function to check the dimensions of a vector
> dim(mat)
[1] 5 4
Dataframes
• Are vectors with dimensions and variable names (attributes)
> df
V1 V2 V3 V4
1 1.31 0.86 -1.44 0.23
2 2.35 -0.15 -1.70 -0.61
3 0.91 1.19 2.02 -2.78
4 -1.06 -1.40 -0.50 1.41
5 -0.54 -0.60 1.72 -1.57
Dataframes
• Can contain different classes
> df
Subject Position Distance
1 Centre 1200
2 Back 1759
3 Forward 1680
Dataframes
• Use the dim function to check dimensions
• Use the names function to check variable names
> dim(df)
[1] 5 4
> names(df)
[[2]]
[1] TRUE
[[3]]
[1] "a"
Attributes
• Names (variable names or dim names)
• Dimensions (nrow, ncol)
• Length (n values if vector with no dim or a matrix, ncol if dataframe)
• Class
Attributes
• Use attributes function to check attributes of a vector
> attributes(df)
$names
$row.names
[1] 1 2 3 4 5
$class
[1] "data.frame"
Missing Values
• Missing values represented by NA
• class( )
• rnorm( )
• c( )
• as.numeric( )
• as.logical( )
• as.character( )
• as.data.frame( )
• matrix( )
• dim( )
• attributes( )
• is.na( )
Basic Functions
Rather than doing this to find the mean..
> (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10) / 10
[1] 5.5
> mean(x)
[1] 5.5
Basic Functions
Some other examples:
• sd( )
• min( )
• max( )
• median( )
• range( )
Basic Functions
Functions have this format...
Example:
mean(x, trim = 0, na.rm = FALSE)
> args(lm)
function (formula, data, subset, weights, na.action, method = "qr", model = TRUE, x =
FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE, contrasts = NULL, offset, ...)
Function Arguments
• You can also use ?function-name to see more information about the function and it’s
arguments
> ?mean
The “...” Argument
• Generic functions use “...” so extra arguments can be passed in the
function later
> args(paste)
function (..., sep = " ", collapse = NULL)
NULL
> paste(“This”, “is”, “Fun”, sep = “ ”, collapse = NULL)
[1] "This is Fun"
The “...” Argument
• The catch – any arguments coming after the “...” must be explicitly named
• Missing values