unexpected symbol

#1

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}

0 Likes

#2

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.

4 Likes

#3

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

0 Likes

#4

@gueyenono

0 Likes

#5

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

1 Like

#6

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

0 Likes

#7

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 }

0 Likes

#8

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

0 Likes

#9

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.

0 Likes

#10

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!

0 Likes

#11

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.

0 Likes

#12

@gueyenono
derinitely. Here is the Screenshot

0 Likes

#13

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

0 Likes

#14

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:

0 Likes

#15

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!

0 Likes

#16

You're very welcome. :slight_smile:

1 Like

#17

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

closed #18

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.

0 Likes