I am trying to calculate the beta for a fund with a customized index in R. The customized index is made from a few different indexes. The index has data from before the fund, so when I am trying to calculate the beta by using
beta <- cov(data$fund,data$index, use =
"pairwise.complete.obs")/var(data$index, use =
"pairwise.complete.obs")
I get the variance from the whole period of the index. The covariance gets correct. I assume that I need to make the customized index to fit the data of the fund, but I can not find any answer on how I am doing this. My data is from latest to newest, so I need the start of the customized index to be NA to where the funds data starts. I need a function that gives me a column with the customized index with only data at the dates where the fund also has data.
Example of the data (random numbers):
Date Fund Customized Index
01.01 NA 100
01.02 50 105
I want it the customized index column to be:
Date Fund Customized Index
01.01 NA NA
01.02 50 105
Update: Solution
I used "ifelse" on making the customized index. This works just as the same way as =IF does in Excel.