0
wmedian <- function(directory, day) {
    files_list <- list.files(directory, full.names = TRUE)
    dat <- data.frame()
    for (i in 1:5) {
            dat <- rbind(dat, read.csv(files_list[i]))
    }
    dat_subset <- dat[which(dat[, "Day"] == day), ]
    median(dat_subset[, "Weight"], na.rm = TRUE)
}

The above code is returning the following error in R when I try to define it ""rror: unexpected input in "wmedian <- function(directory, day) {". I'm actually learning R right now and this code was straight from a tutorial. I tried re-writing the code, and also copy/pasting the code. Both are returning errors and I can't figure out why. What are your thoughts?enter image description here

The goal of this function is to bind a set of csv files together, that have columns "Weight" and "Day". The function binds those files together and return the median of Weight for a given Day.

16
  • 1
    Just to be clear, are you getting the error when you try to define the function, or when you are trying to run the function? It sounds like the former but I am unable to replicate. What version of R are you using (sessionInfo())? Are you running other code before this that may be incomplete? Commented Dec 11, 2014 at 17:58
  • I'm getting the error when I try to define the function. Commented Dec 11, 2014 at 18:01
  • To confirm: I can define the function using R 3.1.1 and RStudio 0.98.1079. That said, it is difficult to debug the function without the CSV files. Could you link to the tutorial? Commented Dec 11, 2014 at 18:02
  • 1
    Closing/re opening worked. Thanks @BondedDust! Does this kind of thing happen frequently in R studio? I'm not sure it's good for my sanity to pour over code for a few hours to find its a software problem. Commented Dec 11, 2014 at 18:40
  • 1
    I've never had a similar problem with RStudio on a PC, although I more frequently use R with the MAC-GUI. I have not seen any other reports that are similar and I am fairly wider read in the R mailing list and SO. It provides many facilities for time-saving so I'd probably stay with it if I were you. Commented Dec 11, 2014 at 18:45

3 Answers 3

4

Try reopen with encoding on the File button, the problem is about your encoding

Sign up to request clarification or add additional context in comments.

2 Comments

I repoened the file and selected UTF-8 (from UTF-8 BOM) and the problem was solved! Thanks :-)
works. I modified the file encoding (<-menu name) in notepad++
3

I got the same result (with the missing 'E').

I managed to solve it by copying piece by piece over to a new editor window and rewriting parts by hand until it all finally ran.

Seems like there was some special character (no idea which one though) that corrupted some very basic lines of code, e.g. library(ggplot2) failed.

Comments

0

I also have the same problem and it was due to the file only having CR at the end of each line instead of CR/LF at the end of each line. The reason the "E" is missing is because for some reason R doesn't know how to process CR on its own, so in the process of trying to print the line, it prints the CR which brings the cursor to the beginning of the line, and then it prints the end quote right on top of the E. So, change the CRs in your file to CR/LF and you should be good.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.