3

I don't know what is happening, but I can't seem to add a constant to a vector. For example, typing in the console c(1,2,3,4)+5 returns 15 instead of (6,7,8,9). What am I doing wrong? Thank you for your help.

wizplum
  • 427
  • 5
  • 17

1 Answers1

14

Someone.... probably you ... has redefined the "+" function. It's easy to do:

> `+` <- function(x,y) sum(x,y)
> c(1,2,3,4)+5
[1] 15

It's easy to fix, Just use rm():

> rm(`+`)
> c(1,2,3,4)+5
[1] 6 7 8 9

EDIT: The comments (which raised the alternate possibility that c had instead been redefined as sum) are prompting me to add information about how to examine and recover from the alternative possibilities. You could use two methods to determine which of the two functions in the expression c(1,2,3,4) + 5 was the culprit. One could either type their names (with the backticks enclosing +), and note whether you got the proper definition:

> `+`
function (e1, e2)  .Primitive("+")
> c
function (..., recursive = FALSE)  .Primitive("c")

Using rm on the culprit (the on that doesn't match above) remains the quickest solution. Using a global rm is an in-session brainwipe:

rm(list=ls())  
# all user defined objects, including user-defined functions will be removed

The advice to quit and restart would not work in some situations. If you quit-with-save, the current function definitions would be preserved. If you had earlier quit-with-save from a session where the redefinition occurred, then not saving in this session would not have fixed the problem, either. The results of prior session are held in a file named ".Rdata and this file is invisible for both Mac and Windows users because the OS file viewer (Mac's Finder.app or MS's Windows Explorer) will not display file names that begin with a "dot". I suspect that Linux users get to see them by default since using ls in a Terminal session will show them. (It's easy to find ways to change that behavior in a Mac, and that is the way I run my device.) Deleting the .Rdata file is helpful in this instance, as well as in the situation where your R session crashes on startup.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • 9
    If you find whoever redefined `+` on your machine, you should redefine `T <- FALSE` and `F <- TRUE` on theirs. – Joshua Ulrich Aug 18 '12 at 15:23
  • 1
    @flodel your answer is more plausible as an accident as DWin's explanation requires malicious intent. If it is DWin's response that's correct I'd go with Joshua's suggestion. – Tyler Rinker Aug 18 '12 at 15:29
  • Thanks, this solved it! Even after reinstalling R this hadn't gone away. Is there any way to clear all the redefines from previous executions? – wizplum Aug 18 '12 at 15:38
  • @user1608941 : the "redefines" are just the items currently loaded, which you can find via `ls()` . Remove them, do `save.image()` , and your .Rdata file will no longer re-source the offending items. – Carl Witthoft Aug 18 '12 at 15:44
  • 1
    You can also use the `conflicts` function to identify which functions have been redefined. – Greg Snow Aug 18 '12 at 17:17
  • I generally dislike the bit of code that wipes clean a workspace, but here it is: `rm(list=ls())` . The other option would be to delete the hidden .Rdata file. – IRTFM Aug 19 '12 at 03:22
  • `list.files(all.files=TRUE)` will show hidden files on any OS. – GSee Aug 19 '12 at 16:02