Error message: unexpected symbol

Hi, I am trying to put this into R, but it keeps telling me I have an "unexpected symbol". it works on other computers, I'm not sure why it won't work on mine. I am using a mac.

ddscale <- function(control, treatment){n <- length(control) control.scaled <- (control - mean(control[1:(n-1)])) / (sd(control[1:(n-1)]))treatment.hyp <- (control.scaled * (sd(treatment[1:(n-1)]) / sd(control.scaled[1:(n-1)])) + (mean(treatment[1:(n-1)]) - mean(control.scaled[1:(n-1)])))treatment.hyp}

Hey @samuel_morrow61,

Welcome to RStudio Community :partying_face::partying_face::partying_face::partying_face::partying_face::partying_face::partying_face::partying_face::partying_face:

I reformatted the function for you. Copy and paste it as it is in your script file:

ddscale <- function(control, treatment){
  n <- length(control) 
  control.scaled <- (control - mean(control[1:(n-1)])) / (sd(control[1:(n-1)]))
  treatment.hyp <- (control.scaled * (sd(treatment[1:(n-1)]) / sd(control.scaled[1:(n-1)])) + (mean(treatment[1:(n-1)]) - mean(control.scaled[1:(n-1)])))
  treatment.hyp
}

Hope this helps.

5 Likes

Thank you! for some reason, it is still showing the "unexpected symbol" message

@gueyenono

Did you realize how your formatting is different from mine? Are you using RStudio?

1 Like

No I did not, I am very new to R. I am using Rstudio

I am copying your code directly into R. It hits me with the same message. Thank you so much for all of your help!

ddscale <- function(control, treatment){ n <- length(control) control.scaled <- (control - mean(control[1:(n-1)])) / (sd(control[1:(n-1)])) treatment.hyp <- (control.scaled * (sd(treatment[1:(n-1)]) / sd(control.scaled[1:(n-1)])) + (mean(treatment[1:(n-1)]) - mean(control.scaled[1:(n-1)]))) treatment.hyp }

I both copied and pasted as well as typed your exact code into R to no avail. It keeps giving me the error message.

My hunch is that you are copying and pasting in the R console. You should create an R script (File > New File > R script) and paste the code in the script instead and run it from there (Ctrl + Enter). Try it and let me know please.

1 Like

No, I am pasting it into a new R Script. The error message is appearing in the console. Ive tried everything I can think of. Thank you so much for all of your help!

1 Like

Hey @samuel_morrow61,

Will you please take a full screen capture of the code pasted in RStudio? Not only a section of it but both the R script and the console and the error.

@gueyenono
derinitely. Here is the Screenshot

That error is happening because for some reason when you paste the code, spaces are getting trimmed, this part is getting pasted together length(control)control.scaled but should be separated length(control) control.scaled

Try to reformat respecting newlines and spaces, like this

That was my point earlier. Your code is on one line and it's not supposed to be. Format it exactly like my answer above and it will work :slight_smile:

1 Like

THANK YOU! Sorry, I am VERY new to R. I took this class to fulfill my stats requirement and I am a history major. Not really my forte. Youre a LIFE SAVER! thank you!

You're very welcome. :slight_smile:

1 Like

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.