What is an object in R?

Name a value with <-, check its type with class(), and multiply whole vectors of prices and share counts at once.

An object in R is a name for a value. You make one with <-.

In this lesson I name a price and a share count, ask R what type each one is, and then value a whole portfolio by multiplying two vectors.

Step 1. Name a value

<- puts the value on the right into the name on the left. R accepts = as well.

price  <- 185.40        # a number with decimals
ticker <- "AAA"        # text, always in quotes
shares <- 10            # how many shares I hold

print(price)            # -> [1] 185.4
[1] 185.4
print(ticker)           # -> [1] "AAA"   <- text prints with its quotes
[1] "AAA"
print(price * shares)   # -> [1] 1854     <- the position valued in one line
[1] 1854

The [1] in front of every result is R telling you the line starts at the first element. Even a single number is a vector of length one:

print(length(price))    # -> [1] 1    <- one number is still a vector
[1] 1
print(length(ticker))   # -> [1] 1    <- and so is one piece of text
[1] 1

Step 2. Ask what type it is

class(x) reports the type of what you stored. Numbers are "numeric", text is "character", TRUE and FALSE are "logical". Adding L to a whole number makes it an "integer".

print(class(price))          # -> [1] "numeric"
[1] "numeric"
print(class(ticker))         # -> [1] "character"
[1] "character"
print(class(shares))         # -> [1] "numeric"     <- 10 is stored with decimals
[1] "numeric"
print(class(10L))            # -> [1] "integer"     <- the L forces a whole number
[1] "integer"
print(class(price > 180))    # -> [1] "logical"     <- a comparison, not a number
[1] "logical"

shares <- 10 gives "numeric", not "integer", because R stores plain numbers with decimals underneath. Write 10L when you want a whole number.

Step 3. A portfolio in two vectors

c() combines several values into one vector. Arithmetic on two vectors runs element by element, so the first price meets the first share count, the second the second. No loop.

tickers <- c("AAA", "CCC", "DDD")  # three names held in one character vector
prices  <- c(185.40, 410.20, 128.75)  # one price per ticker, in that same order
shares  <- c(10, 4, 20)               # the holding that belongs to each ticker

print(tickers)                        # -> [1] "AAA" "CCC" "DDD"
[1] "AAA" "CCC" "DDD"
print(length(prices))                 # -> [1] 3
[1] 3
values <- prices * shares             # 185.40*10, 410.20*4, 128.75*20
print(values)                         # -> [1] 1854.0 1640.8 2575.0
[1] 1854.0 1640.8 2575.0

sum() collapses the vector to a single number. Dividing the vector by it turns the position values into portfolio weights.

total <- sum(values)            # the whole portfolio as one number
print(total)                    # -> [1] 6069.8
[1] 6069.8
weights <- values / total       # each position as a share of the total
print(round(weights, 3))        # -> [1] 0.305 0.270 0.424
[1] 0.305 0.270 0.424
print(sum(weights))             # -> [1] 1
[1] 1

Step 4. What happens when the lengths differ

When one vector is shorter, R repeats it until it matches the longer one.

print(prices * 2)                       # -> [1] 370.8 820.4 257.5   <- one number, reused
[1] 370.8 820.4 257.5
print(c(100, 200, 300, 400) * c(1, 2))  # -> [1] 100 400 300 800     <- c(1, 2) reused twice
[1] 100 400 300 800

The second line multiplied 100 by 1, 200 by 2, then started c(1, 2) again: 300 by 1 and 400 by 2. R printed no warning, because 4 divides evenly by 2.

Your turn

Add a fourth position to the portfolio: 30 shares at a price of 92.50. Rebuild values and weights, and check the weights still sum to 1.

prices  <- c(185.40, 410.20, 128.75, 92.50)  # the fourth price on the end
shares  <- c(10, 4, 20, 30)                  # and the 30 shares that go with it

values  <- prices * shares                   # four positions now, still no loop
print(values)                                # -> [1] 1854.0 1640.8 2575.0 2775.0
print(sum(values))                           # -> [1] 8844.8

weights <- values / sum(values)              # divided by the new, larger total
print(round(weights, 3))                     # -> [1] 0.210 0.186 0.291 0.314
print(sum(weights))                          # -> [1] 1