Table of Contents
Putting things together
Equals sign
If you want to assign something to a variable name, you use the single equals sign =
.
a=4
Notice that when I do this, nothing happens. But, if I type a
into the console, it tells me the value
a ## [1] 4
Any time you want to save the result of something, like the results of InitializeText()
, you have to name it something and use the equals sign to assign the result to the variable.
Double equals sign
This is different than the double equals sign, which asks if two things are equivalent.
a ## [1] 4 a == "apple" ## [1] FALSE a == 4 ## [1] TRUE
Creating vectors
If you want to create your own vector, the simply-named c()
function does the trick
a = c(1, 2, 3) a ## [1] 1 2 3 b = c(4, 5, 6) b ## [1] 4 5 6
Column- and Row-Binding
Sometimes you want to stick two things together. Then cbind()
and rbind()
are helpful. Notice the difference between the two in this example,
cbind(a, b) ## ab ## [1,] 1 4 ## [2,] 2 5 ## [3,] 3 6 rbind(a, b) ## [,1] [,2] [,3] ##a 1 2 3 ##b 4 5 6
Transforming data
One of the great things about R is that it acts like a calculator, and it can be used to transform data very easily. For example, say you wanted to transform the height variable in the cdc
data set from height in meters to height in inches. We know the formula is inches = meters × 39.37, and that’s easy to do in R,
head(cdc$height) ## [1] 1.70 1.75 1.80 1.47 1.83 1.68 heightInches = cdc$height * 39.37 head(heightInches) ## [1] 66.93 68.90 70.87 57.87 72.05 66.14
If you want this new variable to appear as a column in your dataset, just name it like this
cdc$heightInches = cdc$height * 39.37