This is a test version of Biostars. For the public version, visit https://www.biostars.org.
function error in R

Hi,

I was going to write a code but always I get error, may you consider what is my fault here

> 
> my_mean <- function(my_vector){
+ my_vector <- c(2, 4 , 5)
+ sum(my_vector)
+ length(my_vector)
+ my_mean <- function((sum(my_vector)/length(my_vector))
Error: unexpected '(' in:
"length(my_vector)
my_mean <- function(("

in the below is what I was trying to do

# You're free to implement the function my_mean however you want, as long as it
# returns the average of all of the numbers in `my_vector`.
#
# Hint #1: sum() returns the sum of a vector.
#     Ex: sum(c(1, 2, 3)) evaluates to 6
#
# Hint #2: length() returns the size of a vector.
#     Ex: length(c(1, 2, 3)) evaluates to 3
#
# Hint #3: The mean of all the numbers in a vector is equal to the sum of all of
#           the numbers in the vector divided by the size of the vector.
#
# Note for those of you feeling super clever: Please do not use the mean()
# function while writing this function. We're trying to teach you something 
# here!
#
# Be sure to save this script and type submit() in the console after you make 
# your changes.

my_mean <- function(my_vector) {
  # Write your code here!
  # Remember: the last expression evaluated will be returned! 
}
r software-error

Hello Fereshteh!

We believe that this post does not fit the main topic of this site.

Nice you are taking an R-course, but this site is not for R-typos. You should discuss these things with your course mates, or learn to follow instructions. I would like to ask anyone not to try to give further hints or answers in comments to these kind of off-topic questions.

For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.

If you disagree please tell us why in a reply below, we'll be happy to talk about it.

Cheers!

Michael,

since yesterday I stuck on this error, without completing this step I can't go to the next step

my_mean <- function(my_vector) {
  my_vector <- c(2, 4 , 5)
  sum(my_vector)
  length(my_vector)
  my_mean(sum(my_vector)/length(my_vector))
}
my_mean
submit()

how I can see the result of my_mean?

I don't care how long you are stuck with things. We are not an R-support site. You are abusing the site with your series of extremely lazy questions.

I am not abusing, if you think so, then I am really sorry

I found the answer

my_mean <- function(my_vector) {
  sum(my_vector)/length(my_vector)
}
submit()

1 answer

The error is more or less telling you what you did wrong. you just have an extra ( in the line above it.

Can we agree on not resolving these kind of typos any more, also this question should have been closed.

Yeah, that's probably a good policy.

Log in to answer this question.