In one of my homework, I am asked to print the prices of a bond over 2015,2016,2017,2018. Since 2015,2016 have 252 trading days, 2017 with 251 and 2018 with 250, I need to write an if statement. Here is my attempt:
plt <- function(a) {
if (a == 2015) {
x <- 1:252
}
else if (a == 2016) {
x <- 1:252
}
else if (a == 2017) {
x <- 1:251
} else {
x <- 1:250
}
plot(x=x, y=data[data$Year == a, 5], type="l", col="red")
}
However,when I input
function(c(2015, 2016, 2017, 2018))
only draft of 2015 shows up and R returns
Warning messages:
1: In if (a == 2015) { :
the condition has length > 1 and only the first element will be used
2: In data$Year == a :
longer object length is not a multiple of shorter object length
If I input these years seperately, it gives four perfect plots.