Reputation: 1
I am calculating returns in R, and trying to add it to the current dataframe I am working with, but it doesnt work due to a difference in rows, where as existing rows are 194, and assigned data has 193 rows.
This code works just fine when doing it on its own:
diff(log(capm$price_Ford))
But when I try to assign it into the dataframe as its own column, I get the an error
capm$ford_ret <- diff(log(capm$price_Ford))
How can I assign the data with 193 rows, to a dataframe with 194 rows?
Upvotes: 0
Views: 274
Reputation: 545588
How can I assign the data with 193 rows, to a dataframe with 194 rows?
In a nutshell, you can’t. Each column in a table must have the same number of rows. You need to decide what to fill into the row that’s missing a value. Depending on your use-case, this might for example be 0
or NA
. You also need to decide whether the missing value should go at the beginning or at the end (for a difference, usually at the beginning). For example:
capm$ford_ret <- c(NA, diff(log(capm$price_Ford)))
Upvotes: 2