• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/22

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

22 Cards in this Set

  • Front
  • Back

<-

The assignment operator in R.


Ex: x <- 1


print(x)


[1] 1

:

The colon operator creates a sequence, ex:


x <- 1:20


x


[1] 1 2 3 4 5 6 7 8 ...

The five "atomic" classes of objects

character

numeric (real numbers)


integer


complex


logical (True/False, ie, booleans)




vectors

The most basic object. A vector can only contain objects of the same class. The one except is a list, which is represented as a vector but can contain objects of any class.

-Empty vectors can be created with the vector(class, length) function

Numbers in R

Always numeric unless specified. If you want an integer for non-real numbers you need to use the L suffix, eg, 1L.


-NaN represents an undefined value, can be thought as a missing value.

Attributes

R objects can have attributes, for example, names, dimnames, dimensions (matrices, arrays), class, length, or use defined attributes.


-You can access attributes with the attributes() function.

The c() function

This concatenates R objects together. The c function is an easy way to create vectors of objects. Ex:


x <- c(0.5, 0.6);

Mixing Objects & the c function

If you use the c function on mixed objects R will coerce the objects until they are all the same type. It won't throw an error.

Explicit Coercion

You can explicitly coerce objects into different types/classes using the as function. Ex as.numeric(x) or as.logical(x).


-Coercion doesn't always work, and when it doesn't you'll get the NA value.

Lists

The go-to for when you need a container for heterogeneous elements.

Matrices

A special type of vector, not a separate class. Have the dimension attribute, nrow & ncol.

Methods of creating Matrices

1) m <- matrix(1:6, nrow = 2, ncol = 3)


2) x <- 1:3


y <- 10:12


cbind(x, y)


x y


[1,] 1 10


[2,] 2 11


[3,] 3 12


Essentially the same with rbind (row bind)

Factors

A special type of vector, used to represent categorical data. Factors can be unordered or ordered. Think of factors an integer vector where each integer has a label.


-Factors are treated specially by modelling functions, and generally more descriptive / helpful than a more generic type.

Missing Values

These are denoted by either NaN or NA. NaN denotes undefined mathematical operations and NA is for anything else.


You can test for these two using is.na() or is.nan(). is.na() will detect either one.

Data Frames

Used to store tabular data. A special type of list where every element of the list has to have the same length.

Names

An attribute, which can be very useful for writing readable code and self-describing objects.

How to Read Tabular Data

read.table & read.csv

How to Read Data out of a Text File

readLines function

How to Read R Code Files

source & dget

Arguments for read.table

-file, the name of a file or connection


-header, logical (boolean) indicating if the file has a header line


-sep, string indicating how the columns are seperated


-colClasses, a character vector indicating the class of each column in the dataset


-nrows, the number of rows in the dataset

Tips for using read.table

It's helpful to become very familiar with the help page for this function.


-Specify a blank character for the comments arg if there are no comments, will speed up this command.


-Specify a class for the colClasses arg, this will save R the trouble of checking the class each time, greatly speeding up the function.

dump / dput

These two output data into a text format. They retain R metadata.