# R: Swap two variables without using a third

There is general solution or 'trick' for that:

```
a <- 1
b <- 2
a <- a + b
b <- a - b
a <- a - b
```

Here's a useful link that explains a lot: xor-trick

For integers, you can use

```
a = a + b
b = a - b
a = a - b
```

and for strings, this would work

```
a <- "one"
b <- "two"
a <- paste(a,b, sep = "")
b <- substr(a,0,nchar(a) - nchar(b))
a <- substr(a,nchar(b) + 1, nchar(a))
> a
# two
> b
# one
```