R-Objects. An R-Object defines the kind of variable you are working with. Here are the ones we looked at this class.
A scalar is a single number, or “element”, as shown below.
MyScalar = 16A vector is made up of many numbers, or “elements”.
MyVector = c(1, 2, 3, 4, 5, 6, 7, 8, 9)Quick Note: The c() function is the vector function. It combines all the inputs into a vector. Note that all inputs are separated by commas.
Indexing
Indexing is an important concept in all programming languages. Indexing is how you isolate different parts of a larger vector using the location of the element of interest, or the index. Luckily, this is easy in R. To index into a vector, you use square brackets []
MyVector[3]
## [1] 3You can also put a vector inside the square brackets [] using the vector function c( )
MyVector[c(2, 6, 1, 7)]
## [1] 2 6 1 7Data Types
numeric, logical, and binary.
Just the numbers we all know and love.
NumVec = c(1, 45, 367, 43, 234)Logic statements are a vital part of all programming. They are simple TRUE/FALSE or Binary statements, and were traditionally represented as 0 or 1. While most conditional statements accept 0 or 1 (for example, ByRow = 1 and ByRow = TRUE will work the same), to create a logic vector, you have to use TRUE/FALSE statements.
LogVec = c(FALSE, FALSE, TRUE, FALSE, TRUE)Characters, often called strings, and essentialy words.They are defined in most programming langauges using quotation marks " ", and R is no exception.
CharVec = c("Schepens", "Eye", "Research", "Institute")R automatically formats data types differently to make it easier to code. Each data type above appears as a different color in R.R. Instead, R will transform the entire vector into the common denominator. For example, a number can not be represented as a binary statement, but we know that binary statements CAN be represented as numbers. If you try to combine a numeric and logical vector, R will convert it to a numeric vector.Lists are the last R-Object we looked at. Lists are nice because they can organize different data types into a single variable.
MyList = list(Name = "John Smith", Age = 34, Married = TRUE)You can isolate list elements using the dollar sign $.
MyList$Age
## [1] 34
MyList$Name
## [1] "John Smith"
MyList$Married
## [1] TRUEVectors can also be list elements.
PeopleList = list(Name = c("John", "Joe", "Bob"), Age = c(45, 78, 14))
You can index into vectors inside lists by combinging the dollar sign $ with square brackets [ ]
PeopleList$Name[3]
## [1] "Bob"
PeopleList$Age[1]
## [1] 45We wrapped up class talking about functions. Functions are a vital part of programming, they make our lives a lot easier and do basic presets a lot faster than if we did them by hand.
Basic form
FunctioName(input1, input2, option1 = TRUE...)Useful functions
length() returns the length of a vector
length(MyVector)
## [1] 9table() is useful when coutning repeated variables in a vector
Fruit = c("Apple", "Apple", "Pear", "Orange", "Pear", "Apple")
table(Fruit)
## Fruit
## Apple Orange Pear
## 3 1 2