R中的意外符号请参阅代码 [英] unexpected symbol in R please see the code

查看:44
本文介绍了R中的意外符号请参阅代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个函数来解析和合并一些数据.但是 R 抛出了一个意外的符号错误异常.我尝试了不同的方法来解决这个问题,仍然不起作用.请帮忙.

I was trying to write a function to parse and merge some data. But R throws an unexpected symbol error exception. I have tried different ways to solve this issue, still doesn't work. Please help.

查看代码

$aggall = function(df,grp){numcols = sapply(df,class) %in% 
c('integer', 'numeric') result = aggregate(df[,numcols],df[grp],mean) 
 counts = as.data.frame(table(df[grp])) names(counts)[1] = 
 grp merge(counts, result, sort=FALSE)}

错误:aggall = function(go,grp){numcols = sapply(go,class) %in% c('integer','numeric') 结果中的意外符号"

Error: unexpected symbol in "aggall = function(go,grp){numcols = sapply(go,class) %in% c('integer','numeric') results"

推荐答案

您将整个功能集中在一条物理线上.
因此,当 R 试图解析它时,它无法知道一行何时结束,下一行何时开始.

you have your whole function in one physical line.
Therefore, when R tries to parse it, it has no way of knowing when one line ends and the next one begins.

要解决此问题,请使用单独的行或在它们之间添加分号.

To fix this, either use separate lines or add a semicolon between them.

或者,您可以让 formatR 包为您完成!(非常棒的包):

Alternatively, you can have the formatR package do it for you! (pretty awesome package):

install.packages("formatR")
library(formatR)
tidy.source("mySource.R", reindent.space=5)


aggall = function(df, grp) {
     numcols = sapply(df, class) %in% c("integer", "numeric")
     result = aggregate(df[, numcols], df[grp], mean)
     counts = as.data.frame(table(df[grp]))
     names(counts)[1] = grp
     merge(counts, result, sort = FALSE)
} 

这篇关于R中的意外符号请参阅代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆