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 <- "AAPL"        # text, always in quotes
shares <- 10            # how many shares I hold

print(price)            # -> [1] 185.4
[1] 185.4
print(ticker)           # -> [1] "AAPL"
[1] "AAPL"
print(price * shares)   # -> [1] 1854
[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
[1] 1
print(length(ticker))   # -> [1] 1
[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"
[1] "numeric"
print(class(10L))            # -> [1] "integer"
[1] "integer"
print(class(price > 180))    # -> [1] "logical"
[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("AAPL", "MSFT", "NVDA")
prices  <- c(185.40, 410.20, 128.75)
shares  <- c(10, 4, 20)

print(tickers)                  # -> [1] "AAPL" "MSFT" "NVDA"
[1] "AAPL" "MSFT" "NVDA"
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)
print(total)                    # -> [1] 6069.8
[1] 6069.8
weights <- values / 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
[1] 370.8 820.4 257.5
print(c(100, 200, 300, 400) * c(1, 2))  # -> [1] 100 400 300 800
[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)
shares  <- c(10, 4, 20, 30)

values  <- prices * shares
print(values)                 # -> [1] 1854.0 1640.8 2575.0 2775.0
print(sum(values))            # -> [1] 8844.8

weights <- values / sum(values)
print(round(weights, 3))      # -> [1] 0.210 0.186 0.291 0.314
print(sum(weights))           # -> [1] 1