I am trying to calculate the square of the first k
elements of a vector v
.
If k
is greater than the length of the vector n
, then set k = n
.
myfun1 = function(v,k){
p = ifelse(k > length(v), k = length(v), v[1:k]^2)
return(p)
}
myfun1(v=seq(1,20,by=0.5), k=10)
When I run the code it gives me this error:
Error in ifelse(k > length(v), k = length(v), v[1:k]^2) : unused argument (k = length(v))
Can anyone help to resolve this?