简体   繁体   English

我在编写函数时遇到意外的符号错误

[英]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.

相关问题 为什么我在 R 中收到此 function 的意外输入错误? - Why am I getting unexpected input error for this function in R? 我正在尝试将 SAS 代码转换为 R,但我不断收到错误“错误:在“} 中出现意外的‘}’” - I am trying to convert SAS code to R, but I am continuously getting an error "Error: unexpected '}' in " }" 为什么在使用列调用“单独的”function 时出现错误? - Why am I getting an error when calling the `separate` function with a column? 我在 &quot;}&quot;&#39;&#39; 中收到错误 &#39;Error: unexpected &#39;}&#39; 。 我的括号在哪里放错了? - I am getting the error 'Error: unexpected '}' in " }"'' . Where are my brackets misplaced? 尝试运行 function 时出现意外符号错误 - Unexpected symbol error when trying to run a function 我正在定义的函数的“错误:意外符号”,但我无法确定错误的来源 - “Error: unexpected symbol” for a function I'm defining, but I can't identify the source of the error 我想在代码中使用热图,但出现错误 - I want to use heatmap in my code but i am getting error 包装在 function 中并直接运行代码时,我得到了不同的结果 - I am getting different results when wrapped in function and run code directly 我在Rcpp中使用Attributes时遇到错误并且有RcppArmadillo代码 - I am getting error when using Attributes in Rcpp and have RcppArmadillo code 在R中运行LDA函数时不断出现错误,我在使用MASS库作为LDA - Keep getting an error when running the LDA function in R, I am using the MASS library for the LDA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM