Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed subsetting by names, issue #110 #116

Merged
merged 3 commits into from
Apr 7, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions R/Return.portfolio.R
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,17 @@ Return.portfolio <- Return.rebalancing <- function(R,
# make sure that frequency(weights)<frequency(R) ?

# make sure the number of assets in R matches the number of assets in weights
# Should we also check the names of R and names of weights?
# We also check the names of R and names of weights
if(NCOL(R) != NCOL(weights)){
if(NCOL(R) > NCOL(weights)){
if(NCOL(weights)>NCOL(R)){
stop("number of assets is greater than number of columns in returns object")
} else if(is.matrix(weights)&&length(intersect(names(R), colnames(weights)))!=0){
R = R[ , intersect(names(R), colnames(weights))]
} else if(!is.matrix(weights)&&length(intersect(names(R), names(weights)))!=0){
R = R[ , intersect(names(R), names(weights))]
} else {
R = R[, 1:NCOL(weights)]
warning("number of assets in beginning_weights is less than number of columns in returns, so subsetting returns.")
} else {
stop("number of assets is greater than number of columns in returns object")
}
}
} # we should have good weights objects at this point
Expand Down