tv box
tv box

Reputation: 1

R: plotting a function, but "longer object length is not a multiple of shorter object length"

I have witten this simple function in R

> w=c(0.005,0.005,0.006,0.01,0.88,0.03,0.01,0.01,0.005,0.34,0.05)  
> trial<- function(a) {sum(a-w)}  
> trial(0.1)  
[1] 0.996

As you can see this function works fine if I put in a value for a, however I want to plot it and unlike a function not involving sum, the following does not work:

> plot(trial)

Giving me the error:

> Error in xy.coords(x, y, xlabel, ylabel, log) :  
  'x' and 'y' lengths differ  
In addition: Warning message:  
In a - w : longer object length is not a multiple of shorter object length

I understand that R is asking for a vector of length 11, but is there a way to plot this function?

Upvotes: 0

Views: 1248

Answers (2)

IRTFM
IRTFM

Reputation: 263332

I wonder if this is what you wanted:

trial<- function(x) {cumsum(x)}
plot(seq_along(w),  trial(w), type="l")

enter image description here Although it's just a guess. If this is correct (and even if it's not exactly what you wanted) then there are two lessons. 1) Describe the problem fully in natural language; 2) Use functions that return vectors. I think that the cumsum and cumprod functions might not get prominent display in introductory texts. And with them in that situation are also pmin and pmax that are often needed when new users are failing in efforts with max and min.

Also possible this is what you expected:

trial<- function(x) {cumsum(x-w)}
  plot(seq_along(w),  trial(0.1), type="l")

A much more jagged plot results. If smoothness is needed search on smooth.spline.

Upvotes: 1

mnel
mnel

Reputation: 115392

I think the problem lies within your function and what you expect it to give you, and what you have coded.

sum(a-w) will return a single number.

So when you pass a vector (as the plot.function method and underlying curve function will do, it creates a sequence seq(0,1,n=101) (by default).

now your function creates a-w before summing it. Therefore it really only makes sense if is a single value or a vector of the same length as w

trial(seq(0,1,length.out = 101))
[1] 38.331
Warning message:
In x - w : longer object length is not a multiple of shorter object length

It gives an answer, but a warning that you have tried to do something that is perhaps foolish.

The error comes from the fact that when R creates the plot it is creating

plot(seq(0,1,length.out = 101), trial(seq(0,1,length.out = 101))

which will give you the error.

 Error in xy.coords(x, y, xlabel, ylabel, log) : 
 'x' and 'y' lengths differ

So, the question is what do you want your function to return when given vector for a

should it be a- sum(w)? in which case you should really redefine your function to reflect what you want

trial2 <- function(a) {a - sum(w)}

and then

plot(trial2)

will give you a nice (if somewhat uninteresting) straight line

enter image description here

Until you redefine w

w <- 'something else'

plot(trial2)
Error in sum(w) : invalid 'type' (character) of argument

Which is the problem with relying on global variables wthin a function.

Upvotes: 2

Related Questions