Why do I have the error "unexpected symbol" in the cast function?


I have a database in which I use reshape package.

The variables are Date, Company Name, Price.
I made it data.frame and call it Price.Per.Share

My programming was as follows:

md<-melt(Price.Per.Share,id=c("Company Name","Date"))   
a<-cast(md,Date~Company Name)

And it is in this last line that the following error appears:


Error: unexpected symbol in "a

asked by Elvira Durán Martínez 30.11.2018 в 19:28

1 answer


Welcome Elvira. So I see the problem is that Company Name is separated by a space, then R considers that Name is something apart and does not recognize it. The solution would be in the melt to give the name Company_Name , with a low hyphen instead of the space. Alternatively (and more complicated) you could put

'Company Name' 

locked in serious accents or backticks in the cast formula. That is the way to escape spaces and that R evaluate Company Name all together. This also escapes variables that start with a number or that have reserved symbols. The ideal is not to use spaces in the names, they always give work.


If the problem persists, a question with a complete example would be necessary to reproduce the error.

answered by 30.11.2018 в 19:39