R Programming Fundamentals

Seq Function

seq(from = 1, to = 1, by = ((to - from) / (length.out - 1)), 

Unsorted

  • getwd() - returns working directory.
  • setwd(“E:\STA4502\RCode”) - set the working directory to “E:\STA4502\RCode.
  • cbind(x,y) - combine two vectors of same length (x,y) to make a matrix
Our vector will automatically convert our data type if we bind vectors of different data types into a matrix or vector.
  • data.frame(x,y) - combine two vectors of different types into a dataframe while preserving the original variables’ datatypes. Dataframes can handle different length vectors.
  • M = matrix(1:20, nrow = 4, ncol = 5)
  • W = list(Matrix, charVector, intVector) - lists can handle basically all types of parameters.

Cleaning

Ctrl + L

  • clears the R console
    • this does not remove the variables from memory.
  • rm(list=ls()) - this will erase the memory.

Var Types

  1. character
  2. numeric (real numbers)
  3. integer
  4. complex
  5. logical (True/False)

Vectors

Vectors are the most basic R object. An empty vector may be created with vector().

Vectors may only contain objects of the same class.

Lists, while represented as a vector, can break the rule above and contain objects of different classes.

The c() function can be used to create vectors by concatenating things together.

X = c(2.5, -3.5)

Dataframes, Matrices, and Lists

Data frames are standard data objects and are used to combine several variables of the same length and of potentially differing types into a single unit.

D = data.frame(x,y) 
D 
# x   y 
# 11  1
# 218 1
# 123 1
# 36  1
# 10  1

Commenting

Commenting is done like python, with #.

# This is a comment. 

Install Packages

install.package("isdals")

Call Data

To see the collection of all datasets in a package.

data(package = "isdals")

library() function

library() invisibly returns the list of attached packages.

install.packages("ISLR")
library(ISLR)

data(Hitters)

help() function

help() returns a short description of what is included within the data.

help(Hitters)

Source

R Programming Fundamentals

#evergreen