[英]I am getting unexpected symbol error when I code a function
Here is the code 这是代码
reg<-function(y,x){x<-as.matrix(x) x<-cbind(Intercept=1,x) solve(t(x)%*%x)%*%t(x)%*%y}
Error: unexpected symbol in "reg<-function(y,x){x<-as.matrix(x) x" 错误:“ reg <-function(y,x){x <-as.matrix(x)x”中出现意外符号
I get the same error even when I give line breaks and have spaces between the code elements 即使给出换行符并在代码元素之间留有空格,我也会遇到相同的错误
This worked for me: 这为我工作:
reg<-function(y,x){
x<-as.matrix(x);
x<-cbind(Intercept=1,x);
solve(t(x)%*%x)%*%t(x)%*%y;
}
This error is thrown because there is no proper separator after the parse-wise complete statement x<-as.matrix(x)
. 引发此错误的原因是,在按语法分析的完整语句x<-as.matrix(x)
之后没有适当的分隔符。 R's syntax does not recognize a space as a proper separator so the next non-space character needed to be either an end-of-line (EOL) or a semi-colon (;). R的语法无法将空格识别为适当的分隔符,因此下一个非空格字符必须为行尾(EOL)或分号(;)。 I see that Travis already showed a code only solution that used end-of-line separators. 我看到Travis已经展示了使用行尾分隔符的仅代码解决方案。 The one-liner version of your function definiton that would have succeeded is: 函数定义的单线版本将是成功的:
reg<-function(y,x){x<-as.matrix(x); x<-cbind(Intercept=1,x); solve(t(x)%*%x)%*%t(x)%*%y}
Note that this doesn't check for semantic correctness in code but is only a statement by the parser that the syntactic rules have been followed. 请注意,这不会检查代码中的语义正确性,而只是解析器声明已遵循语法规则的声明。 That "unexpected symbol" error message always means that there is an improperly placed or missing parenthesis, curly-brace, semicolon or comma. 该“意外符号”错误消息始终表示存在放置不当或缺少括号,花括号,分号或逗号的情况。 The actual unexpected symbol in this case was x
. 在这种情况下,实际的意外符号是x
。 You can reproduce the error with: 您可以使用以下方法重现该错误:
> x<-as.matrix(x) x
Error: unexpected symbol in "x<-as.matrix(x) x"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.