Reputation: 23
I am new to R and I am trying to create simple UDFs in R. Every time I try to create one, I get the error "
Error: unexpected symbol in:".
Not sure where I am going wrong. Here are a few examples of the functions that I was creating
Function 1
addPercent <- function(x) {
percent <- round (x *100, digits = 1) result<- paste(percent, "%", sep="") return(result)
}
Function 2
avg<- function(x) { s <- sum(x) n <- length(x) s/n }
Would really appreciate any kind of help to solve this minor issue. Thank you much in advance
Upvotes: 2
Views: 217
Reputation: 50728
To expand from my comment:
In R you have to separate statements either with ;
(semicolon) or with a newline.
So this works:
avg <- function(x) { s <- sum(x); n <- length(x); s/n }
avg(c(1, 2, 3))
#[1] 2
As does this
avg <- function(x) {
s <- sum(x)
n <- length(x)
s/n
}
avg(c(1, 2, 3))
#[1] 2
To pre-empt the question "What's the difference?", see the following post: What's the difference in using a semicolon or explicit new line in R code .
Upvotes: 2