Unable to add constant to vector in R

I do not know what is going on, but I cannot add a constant to the vector. For example, input to 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.

+2
source share
1 answer

Someone .... maybe you ... redefined the "+" function. This is easy to do:

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

Easy to fix, just use rm() :

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

EDIT: The comments (which enhanced the alternative possibility that c instead redefined as sum ) prompt me to add information on how to investigate and restore alternative possibilities. You can use two methods to determine which of the two functions in the expression c(1,2,3,4) + 5 was the culprit. You could either type their names (with back windows enclosed in + ), and note whether you had the correct definition:

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

Using rm for the culprit (this does not match above) remains the fastest solution. Using global rm is a brain-shock session:

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

The exit and restart tip will not work in some situations. If you exit saving, the current function definitions will be saved. If you previously left saving from the session where the override occurred, then not saving in this session would not fix the problem either. The results of the previous session are stored in a file named ".Rdata", and this file is invisible to Mac and Windows users, since the OS file viewer (Mac Finder.app or MS Windows Explorer) will not display file names that begin with a period. I suspect that Linux users get them by default, since when using ls in a terminal session they will be displayed. (It's easy to find ways to change this behavior on a Mac, and that is how I launch my device.) Deleting this .Rdata file is useful in this case, as well as in situations where your R session fails to start.

+14
source

All Articles