Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: "Unexpected Symbol" when defining expression

Tags:

r

I'm in a statistics and data analysis class that recently started using R. I'm getting an error message and so far I haven't been able to determine exactly what the error is or how to fix it.

We were given instructions to plot this function:

y=0.1x^4-0.5x^3-x^2+3x-2

The next instruction asks to follow this coding and enter the above function:

> x<-seq(-5,5,by=2)
> y<- ## enter the function, here
> plot(y~x)
> lines(y~x)

This is what I get when I enter the function in y:

> x<-seq(-5, 5, by=2)
> y<-0.1x^4-0.5x^3-x^2+3x-2

Error: unexpected symbol in "y<-0.1x"

Is the unexpected symbol the x? I tried removing the decimals in the function to test it out but get the same error message:

> y<-x^4-5x^3-x^2+3x-2

Error: unexpected symbol in "y<-x^4-5x"

So I'm thinking it's the x that is the problem, but how do I fix it? I ran the x sequence code with no problem.

like image 214
user1771874 Avatar asked Dec 26 '22 15:12

user1771874


2 Answers

In algebra, two symbols next to each other (e.g. 0.1 and x in 0.1x) imply they are multiplied. In programming, that assumption is not made and an explicit multiplication operator is needed: 0.1*x.

like image 184
Brian Diggs Avatar answered Jan 10 '23 03:01

Brian Diggs


Try x * 0.5 instead of x0.5.

Although, I do not use RStudio.

like image 43
Mark Miller Avatar answered Jan 10 '23 02:01

Mark Miller