[Question]
  [
I'm looking for tips for golfing in the R statistical language. R is perhaps an unconventional choice for Golf. However, it does certain things very compactly (sequences, randomness, vectors, and lists), many of the built-in functions have [very short names](https://stackoverflow.com/questions/6979630/what-1-2-letter-object-names-conflict-with-existing-r-objects), and it has an optional line terminator (;). What tips and tricks can you give to help solve code golf problems in R?
  
[Answer]
Some tips:
1. In R, it's recommended to use `<-` over `=`. For golfing, the
opposite holds since `=` is shorter...
2. If you call a function more than once, it is often beneficial to
define a short alias for it:
```
as.numeric(x)+as.numeric(y)
a=as.numeric;a(x)+a(y)
```
3. Partial matching can be your friend, especially when functions
 return lists which you only need one item of. Compare
 `rle(x)$lengths` to `rle(x)$l`
4. Many challenges require you to read input. `scan` is often a good fit for this (the user ends the input by entring an empty line).
```
scan()    # reads numbers into a vector
scan(,'') # reads strings into a vector
```
5. Coercion can be useful. `t=1` is much shorter than `t=TRUE`. Alternatively, `switch` can save you precious characters as well, but you'll want to use 1,2 rather than 0,1.
```
if(length(x)) {} # TRUE if length != 0
sum(x<3)         # Adds all the TRUE:s (count TRUE)
```
6. If a function computes something complicated and you need various other types of calculations based on the same core value, it is often beneficial to either: a) break it up into smaller functions, b) return all the results you need as a list, or c) have it return different types of values depending on an argument to the function.
7. As in any language, know it well - R has thousands of functions, there is probably some that can solve the problem in very few characters - the trick is to know which ones!
Some obscure but useful functions:
```
sequence
diff
rle
embed
gl # Like rep(seq(),each=...) but returns a factor
```
Some built-in data sets and symbols:
```
letters     # 'a','b','c'...
LETTERS     # 'A','B','C'...
month.abb   # 'Jan','Feb'...
month.name  # 'January','Feburary'...
T           # TRUE
F           # FALSE
pi          # 3.14...
```
[Answer]
1. Instead of importing a package with `library`, grab the variable from the package using `::` . Compare the followings:
```
library(splancs);inout(...)
splancs::inout(...)
```
Of course, it is only valid if one single function is used from the package.
2. This is trivial but a rule of thumb for when to use @Tommy's trick of aliasing a function: if your function name has a length of `m` and is used `n` times, then alias only if `m*n > m+n+3` (because when defining the alias you spend `m+3` and then you still spend 1 everytime the alias is used). An example:
```
nrow(a)+nrow(b)     # 4*2 < 4+3+2
n=nrow;n(a)+n(b)
length(a)+length(b) # 6*2 > 6+3+2
l=length;l(a)+l(b)
```
3. Coercion as side-effect of functions: 
	* instead of using `as.integer`, character strings can be coerced to integer using `:` :
	
	
	
	```
	as.integer("19")
	("19":1)[1] #Shorter version using force coercion.
	
	```
	* integer, numeric, etc. can be similarly coerced to character using `paste` instead of `as.character`:
	
	
	
	```
	as.character(19)
	paste(19) #Shorter version using force coercion.
	
	```
[Answer]
Some very specific golfing tips:
* if you need to extract the length of a vector, `sum(x|1)` is shorter than `length(x)` as long as `x` is numeric, integer, complex or logical.
* if you need to extract the last element of a vector, it may be cheaper (if possible) to initialise the vector *backwards* using `rev()` and then calling `x[1]` rather than `x[length(x)]` (or using the above tip, `x[sum(x|1)]`) (or `tail(x,1)` --- thanks Giuseppe!). A slight variation on this (where the second-last element was desired) can be seen [here](https://codegolf.stackexchange.com/a/163922/55516). Even if you can't initialise the vector backwards, `rev(x)[1]` is still shorter than `x[sum(x|1)]` (and it works for character vectors too). Sometimes you don't even need `rev`, for example using `n:1` instead of `1:n`.
* (As seen [here](https://codegolf.stackexchange.com/a/96915/55516)). If you want to coerce a data frame to a matrix, don't use `as.matrix(x)`. Take the transpose of the transpose, `t(t(x))`.
* `if` is a formal function. For example, `"if"(x<y,2,3)` is shorter than `if(x<y)2 else 3` (though of course, `3-(x<y)` is shorter than either). This only saves characters if you don't need an extra pair of braces to formulate it this way, which you often do.
* For testing non-equality of numeric objects, `if(x-y)` is shorter than `if(x!=y)`. Any nonzero numeric is regarded as `TRUE`. If you are testing equality, say, `if(x==y)a else b` then try `if(x-y)b else a` instead. Also see the previous point.
* The function `el` is useful when you need to extract an item from a list. The most common example is probably `strsplit`: `el(strsplit(x,""))` is one fewer byte than `strsplit(x,"")[[1]]`.
* (As used [here](https://codegolf.stackexchange.com/a/97389/55516)) Vector extension can save you characters: if vector `v` has length `n` you can assign into `v[n+1]` without error. For example, if you wanted to print the first ten factorials you could do: `v=1;for(i in 2:10)v[i]=v[i-1]*i` rather than `v=1:10:for(...)` (though as always, there is another, better, way: `cumprod(1:10)`)
* Sometimes, for text based challenges (particularly 2-D ones), it's easier to `plot` the text rather than `cat` it. the argument `pch=` to `plot` controls which characters are plotted. This can be shortened to `pc=` (which will also give a warning) to save a byte. Example [here](https://codegolf.stackexchange.com/a/91930/55516).
* To take the floor of a number, don't use `floor(x)`. Use `x%/%1` instead.
* To test if the elements of a numeric or integer vector are all equal, you can often use `sd` rather than something verbose such as `all.equal`. If all the elements are the same, their standard deviation is zero (`FALSE`) else the standard deviation is positive (`TRUE`). Example [here](https://codegolf.stackexchange.com/a/146481/55516).
* Some functions which you would expect to require integer input actually don't. For example, `seq(3.5)` will return `1 2 3` (the same is true for the `:` operator). This can avoid calls to `floor` and sometimes means you can use `/` instead of `%/%`.
* The most common function for text output is `cat`. But if you needed to use `print` for some reason, then you might be able to save a character by using `show` instead (which in most circumstances just calls `print` anyway though you forego any extra arguments like `digits`)
* don't forget about complex numbers! The functions to operate on them (`Re`, `Im`, `Mod`, `Arg`) have quite short names which can occasionally be useful, and complex numbers as a concept can sometimes yield simple solutions to some calculations.
* for functions with very long names (>13–15 characters), you can use `get` to get at the function. For example, in R 3.4.4 with no packages loaded other than the default, `get(ls(9)[501])` is more economical than `getDLLRegisteredRoutines`. This can also get around source code restrictions such as [this answer](https://codegolf.stackexchange.com/a/90666/55516). Note that using this trick makes your code R-version-dependent (and perhaps platform dependent), so make sure you include the version in your header so it can be reproduced if necessary.
[Answer]
1. Abuse the builtins `T` and `F`. By default, they evaluate to `TRUE` and `FALSE`, which can be automatically converted to numerics `1` and `0`, and they can be re-defined at will. This means that you don't need to initialize a counter (e.g. `i=0` ... `i=i+1`), you can just use `T` or `F` as needed (and jump straight to `F=F+1` later).
2. Remember that functions return the last object called and do not need an explicit `return()` call.
3. Defining short aliases for commonly used functions is great, such as `p=paste`. If you use a function a lot, and with *exactly two* arguments, it is possible that an *infixing* alias will save you some bytes. Infixing aliases must be surrounded by `%`. For example:
```
`%p%`=paste
```
And subsequently `x%p%y`, which is 1 byte shorter than `p(x,y)`. The infixing alias definition is 4 bytes longer than the non-infixing `p=paste` though, so you have to be sure it's worth it.
[Answer]
# Using `if`, `ifelse`, and ``if``
There are several ways to do if-statements in R. Golf-optimal solutions can vary a lot.
### The basics
1. `if` is for control flow. It is not vectorized, i.e. can only evaluate conditions of length 1. It requires `else` to (optionally) return an else value.
2. `ifelse` is a function. It is vectorized, and can return values of arbitrary length. Its third argument (the else value) is obligatory.\*
3. ``if`` is a function, with the same syntax as `ifelse`. It is not vectorized, nor are any of the return arguments obligatory.
\* It's not technically obligatory; `ifelse(TRUE,x)` works just fine, but it throws an error if the third argument is empty and the condition evaluates to `FALSE`. So it's only safe to use if you are sure that the condition is always `TRUE`, and if that's the case, why are you even bothering with an if-statement?
### Examples
These are all equivalent:
```
if(x)y else z # 13 bytes
ifelse(x,y,z) # 13 bytes
`if`(x,y,z)   # 11 bytes
```
Note that the spaces around `else` are not required if you are using strings directly in the code:
```
if(x)"foo"else"bar"   # 19 bytes
ifelse(x,"foo","bar") # 21 bytes
`if`(x,"foo","bar")   # 19 bytes
```
So far, ``if`` looks to be the winner, as long as we don't have vectorized input. But what about cases where we don't care about the else condition? Say we only want to execute some code if the condition is `TRUE`. For one line of code alone, `if` is usually best:
```
if(x)z=f(y)         # 11 bytes
ifelse(x,z<-f(y),0) # 19 bytes
`if`(x,z<-f(y))     # 15 bytes
```
For multiple lines of code, `if` is still the winner:
```
if(x){z=f(y);a=g(y)}        # 20 bytes
ifelse(x,{z=f(y);a=g(y)},0) # 27 bytes
`if`(x,{z=f(y);a=g(y)})     # 23 bytes
```
There's also the possibility where we *do* care about the else condition, and where we want to execute arbitrary code rather than return a value. In these cases, `if` and ``if`` are equivalent in byte count.
```
if(x)a=b else z=b   # 17 bytes
ifelse(x,a<-b,z<-b) # 19 bytes
`if`(x,a<-b,z<-b)   # 17 bytes
if(x){z=y;a=b}else z=b   # 22 bytes
ifelse(x,{z=y;a=b},z<-b) # 24 bytes
`if`(x,{z=y;a=b},z<-b)   # 22 bytes
if(x)a=b else{z=b;a=y}   # 22 bytes
ifelse(x,a<-b,{z=b;a=y}) # 24 bytes
`if`(x,a<-b,{z=b;a=y})   # 22 bytes
if(x){z=y;a=b}else{z=b;a=y}   # 27 bytes
ifelse(x,{z=y;a=b},{z=b;a=y}) # 29 bytes
`if`(x,{z=y;a=b},{z=b;a=y})   # 27 bytes
```
### Summary
1. Use `ifelse` when you have input of length > 1.
2. If you're returning a simple value rather than executing many lines of code, using the ``if`` function is probably shorter than a full `if`...`else` statement.
3. If you just want a single value when `TRUE`, use `if`.
4. For executing arbitrary code, ``if`` and `if` are usually the same in terms of byte count; I recommend `if` mainly because it's easier to read.
[Answer]
1. You can assign a variable to the current environment while simultaneously supplying it as an argument to a function:
```
sum(x <- 4, y <- 5)
x
y
```
2. If you are subseting a `data.frame` and your condition depends on several of its columns, you can avoid repeating the `data.frame` name by using `with` (or `subset`).
```
d <- data.frame(a=letters[1:3], b=1:3, c=4:6, e=7:9)
with(d, d[a=='b' & b==2 & c==5 & e==8,])
```
instead of 
```
d[d$a=='b' & d$b==2 & d$c==5 & d$e==8,]
```
Of course, this only saves characters if the length of your references to the `data.frame` exceeds the length of `with(,)`
3. `if...else` blocks can return the value of the final statement in which ever part of the block executes. For instance, instead of
```
a <- 3
if (a==1) y<-1 else
if (a==2) y<-2 else y<-3
```
you can write 
```
y <- if (a==1) 1 else 
     if (a==2) 2 else 3
```
[Answer]
## Do-while loops in R
Occasionally, I find myself wishing R had a `do-while` loop, because:
```
 some_code
while(condition){
 some_code # repeated
}
```
is far too long and very un-golfy. However, we can recover this behavior and shave off some bytes with the power of the `{` function.
`{` and `(` are each `.Primitive` functions in R.
The documentation for them reads:
> 
> Effectively, `(` is semantically equivalent to the identity `function(x) x`, whereas `{` is slightly more interesting, see examples.
> 
> 
> 
and under Value,
> 
> For `(`, the result of evaluating the argument. This has visibility set, so will auto-print if used at top-level.
> 
> 
> For `{`, **the result of the last expression evaluated**. This has the visibility of the last evaluation.
> 
> 
> 
(emphasis added)
So, what does this mean? It means a do-while loop is as simple as
```
while({some_code;condition})0
```
because the expressions inside `{}` are each evaluated, and only the *last one* is returned by `{`, allowing us to evaluate `some_code` before entering the loop, and it runs each time `condition` is `TRUE` (or truthy). The `0` is one of the many 1-byte expressions that forms the "real" body of the `while` loop.
Additionally, this can be combined with `print` (which invisibly returns its argument) to repeatedly print an intermediate result until (and including) it reaches `0`. An example of this can be found [here](https://codegolf.stackexchange.com/a/218160/67312).
[Answer]
1. **Save values in-line**: Others have mentioned that you can pass values in-order and assign them for use elsewhere, i.e.
```
 sum(x<- 1:10, y<- seq(10,1,2))
```
However, you can also *save* values inline for use *in the same line*!
For instance
```
    n=scan();(x=1:n)[abs(x-n/2)<4]
```
reads from `stdin`, creates a variable `x=1:n`, then indexes into `x` using that value of `x`. This can sometimes save bytes.
2. **Alias for the empty vector** You can use `{}` as the empty vector `c()` as they both return `NULL`.
3. **Base Conversion** For integer digits of `n` in base 10, use `n%/%10^(0:nchar(n))%%10`. This will leave a trailing zero, so if that is important to you, use `n%/%10^(1:nchar(n)-1)%%10` ot `n%/%10^(0:log10(n))`. This can be adapted to other bases, using `0:log(n,b)` instead of `log10(n)`.
4. **Using `seq` and `:`**: Rather than using `1:length(l)` (or `1:sum(x|1)`), you can use `seq(l)` as long as `l` is a `list` or `vector` of length greater than 1, as it defaults to `seq_along(l)`. If `l` is numeric and could potentially be length `1`, `seq(!l)` or `seq(a=l)` will [do the trick](https://codegolf.stackexchange.com/questions/206967/sum-the-array-times-n-except-the-last/207064#207064).
Additionally, `:` will (with a warning) use the first element of its arguments.
5. **Removing attributes** Using `c()` on an `array` (or `matrix`) will do the same as `as.vector`; it generally removes non-name attributes.
6. **Factorial** Using `gamma(n+1)` is shorter than using `factorial(n)` and `factorial` is defined as `gamma(n+1)` anyway.
7. **Coin Flipping** When needing to do a random task 50% of the time, using `rt(1,1)<0` is shorter than `runif(1)<0.5` by three bytes.
8. **Extracting/Excluding elements** `head` and `tail` are often useful to extract the first/last few elements of an array; `head(x,-1)` extracts all but the last element and is shorter than using negative indexing, if you don't already know the length:
```
head(x,-1)
x[-length(x)]
x[-sum(x|1)]
```
[Answer]
# Implicit type conversion
The functions `as.character`, `as.numeric`, and `as.logical` are too byte-heavy. Let's trim them down.
## Conversion to logical from numeric (4 bytes)
Suppose `x` is a numeric vector. Using the logical not operator `!` implicitly recasts the numeric to a logical vector, where `0` is `FALSE` and nonzero values are `TRUE`. `!` then inverts that.
```
x=!x
```
`x=0:3;x=!x` returns `TRUE FALSE FALSE FALSE`.
## Conversion to character from numeric or logical (7 bytes)
This is a fun one. (From [this tweet](https://twitter.com/johnmyleswhite/status/986335685585133568).)
```
x[0]=''
```
R sees that you're updating the vector `x` with `''`, which is of class `character`. So it casts `x` into class `character` so it's compatible with the new data point. Next, it goes to put `''` in the appropriate place... but the index `0` doesn't exist (this trick also works with `Inf`, `NaN`, `NA`, `NULL`, and so on). As a result, `x` is modified in class only.
`x=1:3;x[0]=''` returns `"1" "2" "3"`, and `x=c(TRUE,FALSE);x[0]=''` returns `"TRUE" "FALSE"`.
If you have a character object already defined in your workspace, you can use that instead of `''` to save a byte. E.g., `x[0]=y`!
## Conversion to character from numeric or logical under certain conditions (6 bytes)
[J.Doe](https://codegolf.stackexchange.com/users/81549/j-doe) pointed out in the comments a six-byte solution:
```
c(x,"")
```
This works if `x` is atomic and if you intend to pass it to a function which requires an atomic vector. (The function may throw a warning about ignoring elements of the argument.)
# Conversion to numeric from logical (4 bytes)
You can use the funky indexing trick from above (e.g. `x[0]=3`), but there's actually a quicker way:
```
x=+x
```
The positive operator implicitly recasts the vector as a numeric vector, so `TRUE FALSE` becomes `1 0`.
[Answer]
1. Abuse `outer` to apply an arbitrary function to all the combinations of two lists. Imagine a matrix with i, j indexed by the first args, then you can define an arbitrary function(i,j) for each pair.
2. Use `Map` as a shortcut for `mapply`. My claim is that `mapply` is cheaper than a for loop in situations where you need to access the index. Abuse the list structure in R. `unlist` is expensive. `methods::el` allows you to cheaply unlist the first element. Try to use functions with list support natively.
3. Use `do.call` to generalize function calls with arbitrary inputs.
4. The accumulate args for `Reduce` is extremely helpful for code golf.
5. Writing to console line by line with `cat(blah, "\n")` is cheaper with `write(blah, 1)`. Hard coded strings with "\n" may be cheaper in some situations.
6. If a function comes with default arguments, you can use function(,,n-arg) to specify the n-th argument directly. Example:`seq(1, 10, , 101)` In some functions, partial argument matching is supported. Example: `seq(1, 10, l = 101)`.
7. If you see a challenge involving string manipulation, just press the back button and read the next question. `strsplit` is single handily responsible for ruining R golf.
Now for some newly discovered tips from 2018
8. `A[cbind(i,j)] = z` can be a good way to manipulate matrices. This operation is very byte efficient assuming you design `i, j, z` as vectors with correct lengths. You may save even more by calling the actual index/assign function `"[<-"(cbind(i,j), z)`. This way of calling returns the modified matrix.
9. Use a new line instead of `\n` for line breaks.
10. Squeezing down line counts can save you bytes. In-line assignment `lapply(A<-1:10,function(y) blah)` and function args assignment `function(X, U = X^2, V = X^3)` are ways of doing this.
11. So `"[<-"` is a function in R (and is related to [my ancient question on SO](https://stackoverflow.com/questions/27515050/is-there-a-way-to-call-the-function-in-form))! That is the underlying function responsible for operations such as `x[1:5] = rnorm(5)`. The neat property of calling the function by name allows you to return the modified vector. In order words `"[<-"(x, 1:5, normr(5))` does almost the same thing as the code above except it returns the modified x. The related "length<-", "names<-", "anything<-" all return modified output
[Answer]
# Change the meaning of operators
R operators are just functions that get special treatment by the parser. For example `<` is actually a function of two variables. These two lines of code do the same thing:
```
x < 3
`<`(x, 3) 
```
You can reassign another function to an operator, and the parser will still do it's thing, including respecting operator precedence, but the final function call will be the new one rather than the original. For example:
```
`<`=rep
```
now means these two lines of code do the same thing:
```
rep("a", 3)
"a"<3
```
and precedence is respected, resulting in things like
```
"a"<3+2
#[1] "a" "a" "a" "a" "a"
```
See for example [this answer](https://codegolf.stackexchange.com/a/164336/80010), and also [the operator precedence page](https://stat.ethz.ch/R-manual/R-devel/library/base/html/Syntax.html). As a side effect, your code will become as cryptic as one written in a golf language.
Some operators like `+` and `-` can accept either one or two parameters, so you can even do things like:
```
`-`=sample
set.seed(1)
-5  # means sample(5)
#[1] 2 5 4 3 1
5-2 # means sample(5, 2)
#[1] 5 4
```
See for example [this answer](https://codegolf.stackexchange.com/a/171490/79980).
See also [this answer](https://codegolf.stackexchange.com/a/185021/80010) for using `[` as a two-byte, three-argument operator.
[Answer]
# Scenarios where you can avoid `paste(...,collapse="")` and `strsplit`
These are a pain in the usual string challenges. There are some workarounds.
* `Reduce(paste0,letters)` for -5 bytes from `paste0(letters,collapse="")`
* A [2-byte golf](https://codegolf.stackexchange.com/questions/70837/say-what-you-see/104416#comment414550_104416) where you have a list containing two vectors `c(1,2,3)` and `c(4,5,6)` and want to concatenate them element-wise to a string `"142536"`. Operator abuse gives you `p=paste0;"^"=Reduce;p^p^r` which saves two bytes on the usual `paste0` call.
* Instead of `paste0("(.{",n,"})")` to construct (eg) a regex for 20 bytes, consider a regex in a regex: `sub(0,"(.{0})",n)` for 17 bytes.
Sometimes (quite often, actually) you'll need to iterate through a vector of characters or strings, or split a word into letters. There are two common use cases: one where you need to take a vector of characters as input to a function or program, and one where you know the vector in advance and need to store it in your code somewhere.
a. Where you need to take a string as **input** and split it into either *words* or *characters*.
1. If you need **words** (including characters as a special case):
	* If a newline `0x10` (ASCII 16) separating the words is OK, `x=scan(,"")` is preferred to wrapping your code in `function(s,x=el(strsplit(s," ")))`.
	* If the words can be separated by **any other whitespace**, including multiple spaces, tabs, newlines etc, you can use @ngm's [**double scan trick**](https://codegolf.stackexchange.com/revisions/b356bbc3-90b6-41cd-91c0-7152e47d314b/view-source): `x=scan(,"",t=scan(,""))`. This gives the scanned in string to `scan` as the `text` arg and separates it by whitespace.
	* The second argument in `scan` can be any string so if you have created one, you can [recycle it](https://codegolf.stackexchange.com/a/173514/81549) to save a byte.
2. If you need to turn an input string into a **vector of characters**:
	* `x=el(strsplit(s,""))` is the shortest *general* solution. The `split` argument works on anything of length zero including `c()`, `{}` etc so if you happen to have created a zero length variable, you could use it to save a byte.
	* If you can work with the ASCII character codes, consider `utf8ToInt`, since `utf8ToInt(x)` is shorter than the `strsplit` call. To paste them back together, `intToutf8(utf8ToInt(x))` is shorter than `Reduce(paste0,el(strsplit(x,"")))`.
	* If you need to split arbitrary strings of numbers like `"31415926535"` as input, you can use `utf8ToInt(s)-48` to save 3 bytes on `el(strsplit(s,""))`, provided you can use the integer digits instead of the characters, as is often the case. This is also shorter than the usual recipe for splitting numbers into decimal digits.
b. Where you need a **fixed vector** of either words or characters in advance.
* If you need a vector of single characters that have some regular pattern or are in alphabetic order, look at using `intToUtf8` or `chartr` applied to a sequence via `a:b` or on the built in letters sets `letters` or `LETTERS`. The pattern language built into `chartr` is [especially powerful](https://codegolf.stackexchange.com/a/169316/81549).
* For **1 to 3 characters or words**, `c("a","b","c")` is the only general shortest solution.
* If you need a fixed vector of **between 4 and 10 non whitespace characters or words**, use `scan` with `stdin` as the `file` arg:
```
f(x=scan(,""))
q
w
e
r
t
y
u
```
* If `scan` from `stdin` isn't possible, for **6 or more non whitespace characters or words**, use `scan` with the `text` argument `scan(,"",t="a b c d e f")`.
* If you need a vector of (a) **6 or more characters of any type** or (b) **10 or more non-whitespace characters** , `strsplit` via `x=el(strsplit("qwertyuiop",""))` is probably the way to go.
* You *may* be able to get away with the [following **quote trick**](https://codegolf.stackexchange.com/a/173161/81549): `quote(Q(W,E,R,T,Y))`, which creates that expression. Some functions like `strrep`, and `grep` will coerce this to a vector of strings! If you do, this is good for any length of word or character vector from 3 to 11.
* There's no good reason to use `strsplit` on words via `x=el(strsplit("q w e r t y"," "))`. It always loses to `scan(,"",t="q w e r t y"))` by a fixed overhead of 5 bytes.
Here's a table of the byte counts used by each approach to read in a vector of single characters of length `n`. The relative ordering within each row is valid for characters or words, except for `strsplit` on `""` which only works on characters.
```
| n  | c(...) | scan | scan | strsplit | quote |
|    |        |+stdin|+text | on ""    | hack  |
|    |        |      |      | CHAR ONLY|       |
|----|--------|------|------|----------|-------|
| 1  | 3      | 11   | 15   | 20       | 8     |
| 2  | 10     | 13   | 17   | 21       | 11    |
| 3  | 14     | 15   | 19   | 22       | 13    |
| 4  | 18     | 17   | 21   | 23       | 15    |
| 5  | 22     | 19   | 23   | 24       | 17    |
| 6  | 26     | 21   | 25   | 25       | 19    |
| 7  | 30     | 23   | 27   | 26       | 21    |
| 8  | 34     | 25   | 29   | 27       | 23    |
| 9  | 38     | 27   | 31   | 28       | 25    |
| 10 | 42     | 29   | 33   | 29       | 27    |
| 11 | 46     | 31   | 35   | 30       | 29    |
| 12 | 50     | 33   | 37   | 31       | 31    |
```
c. If you need to input **text as a character matrix**, a few recipes that seem short are
```
s="hello\nworld\n foo"
# 43 bytes, returns "" padded data frame
# If lines > 5 are longer than lines <= 5, wraps around and causes error
read.csv(t=gsub("(?<=.)(?=.)",",",s,,T),,F)
# 54 bytes with readLines(), "" padded matrix
sapply(p<-readLines(),substring,p<-1:max(nchar(p)),p))
# plyr not available on TIO
# 58 bytes, returns NA padded matrix, all words split by whitespace
plyr::rbind.fill.matrix(Map(t,strsplit(scan(,"",t=s),"")))
# 61 bytes, returns NA padded matrix
plyr::rbind.fill.matrix(Map(t,(a=strsplit)(el(a(s,"\n")),"")))
```
[Answer]
Some basic concepts but should be somewhat useful:
1. In control flow statements you can abuse that any number not equal to zero will be evaluated as `TRUE`, e.g.: `if(x)` is equivalent to `if(x!=0)`. Conversely, `if(!x)` is equivalent to `if(x==0)`.
2. When generating sequences using `:` (e.g. `1:5`) one can abuse the fact that the exponentiation operator `^` is the only operator that has precedence over the `:`-operator (as opposed to `+-*/`).
```
1:2^2 => 1 2 3 4 
```
which saves you two bytes on the parentheses that you would normally have to use in case you wanted to e.g. loop over the elements of an `n x n` matrix (`1:n^2`) or any other integer that can be expressed in a shorter manner using exponential notation (`1:10^6`).
3. A related trick can of course be used on the vectorized operations as well `+-*/`, although most commonly applicaple to `+-`:
```
for(i in 1:(n+1)) can instead be written as for(i in 0:n+1)
```
This works because `+1`is vectorized and adds `1` to each element of `0:n` resulting in the vector `1 2 ... n+1`. Similarly `0:(n+1) == -1:n+1` saves you one byte as well.
4. When writing short functions (that can be expressed on one line), one can abuse variable assignment to save two bytes on the enclosing curly brackets `{...}`:
```
f=function(n,l=length(n))for(i in 1:l)cat(i*l,"\n")
f=function(n){l=length(n);for(i in 1:l)cat(i*l,"\n")}
```
Note that this might not always comply to rules of certain challenges.
[Answer]
# Alternatives to `rep()`
Sometimes `rep()` can be avoided with the colon operator `:` and R's vector recycling. 
* For repeating `n` zeroes, where `n>0`, `0*1:n` is 3 bytes shorter than `rep(0,n)` and `!1:n`, an array of `FALSE`, is 4 bytes shorter, if the use case allows it.
* To repeat `x` `n` times, `x+!1:n` is 2 bytes shorter than `rep(x,n)`. For `n` ones, use `!!1:n` if you can use an array of `TRUE`.
* To repeat `x` `2n+1` times, where `n>=0`, `x+0*-n:n` is 4 bytes shorter than `rep(x,2*n+1)`.
* The statement `!-n:n` will give a `TRUE` flanked on both sides by `n` `FALSE`. This [can be used](https://codegolf.stackexchange.com/questions/167405/alpha-numerical-bowtie#comment414849_167413) to generate *even* numbers of characters in calls to `intToUtf8()` if you remember that a zero is ignored.
Modular arithmetic can be useful. `rep` statements with the `each` argument can sometimes be avoided using integer division. 
* To generate the vector `c(-1,-1,-1,0,0,0,1,1,1)`, `-3:5%/%3` is 5 bytes shorter than `rep(-1:1,e=3)`.
* To generate the vector `c(0,1,2,0,1,2,0,1,2)`, `0:8%%3` saves 4 bytes on `rep(0:2,3)`.
* Sometimes nonlinear transformations can shorten sequence arithmetic. To map `i in 1:15` to `c(1,1,3,1,1,3,1,1,3,1,1,3,1,1,3)` inside a compound statement, the obvious golfy answer is `1+2*(!i%%3)` for 11 bytes. However, `3/(i%%3+1)` is [10 bytes](https://codegolf.stackexchange.com/questions/58615/1-2-fizz-4-buzz?page=1&tab=votes#comment417712_58637), and will floor to the same sequence, so it can be used if you need the sequence for array indexing.
[Answer]
When you do need to use a function, use `pryr::f()` instead of `function()`.
Example:
```
function(x,y){x+y}
```
is equivalent to 
```
pryr::f(x,y,x+y)
```
or, even better, 
```
pryr::f(x+y)
```
Since If there is only one argument, [the formals are guessed from the code](https://cran.r-project.org/web/packages/pryr/pryr.pdf).
[Answer]
# Surviving challenges involving strings
As mentioned in another answer, `unlist(strsplit(x,split="")` and `paste(...,collapse="")` can be depressing. But don't just walk away from these, there are workarounds!
* `utf8ToInt` converts a string to a vector, `intToUtf8` does the reverse operation. You're getting a vector of `int`, not a vector of `char` but sometimes this is what you're looking for. For instance to generate a list of `-`, better use`intToUtf8(rep(45,34))` than `paste(rep("-",34),collapse="")`
* `gsub` is more useful than other function of the `grep` family when operating on a single string.
The two approaches above can be combined as in [this answer](https://codegolf.stackexchange.com/a/164368/80010) which benefited from the advice of [ovs](https://codegolf.stackexchange.com/users/64121/ovs), [Giuseppe](https://codegolf.stackexchange.com/users/67312/giuseppe) and [ngm](https://codegolf.stackexchange.com/users/79980/ngm).
* Choose a convenient I/O format as in [this answer](https://codegolf.stackexchange.com/a/157613/80010) taking input as lines of text (without quotes) or [this one](https://codegolf.stackexchange.com/a/164106/80010) taking a vector of chars. Check with the OP when in doubt.
* As pointed out in the comments, `<` compares strings lexicographically as one would expect.
[Answer]
# R 4.1.0: new shorthand function syntax
I just found out the very new release R 4.1.0 comes with some new syntax. <https://www.r-bloggers.com/2021/05/new-features-in-r-4-1-0/>
The new shorthand syntax `\(arglist) expr` performs exactly as `function(arglist) expr` and makes writing R functions 7 bytes shorter:
```
\(x,y)x+y
```
instead of
```
function(x,y)x+y
```
There's also the new native pipe `|>`, but this isn't shorter than just using parentheses (and the pipe has to be used with parentheses, unlike magrittr.)
[Answer]
Some ways to find the first non-zero element of an array.
If it has a name `x`:
```
x[!!x][1]
```
Returns `NA` if no non-zero elements (including when `x` is empty, but not `NULL` which errors.)
Anonymously:
```
Find(c, c(0,0,0,1:3))
```
Returns `NULL` if no non-zero elements, or empty or `NULL`. 
[Answer]
# Tips for restricted source challenges :
1. Characters in R literals constants can be replaced by hex codes, octal codes and unicodes. 
e.g. the string `"abcd"` can be written :
```
    # in octal codes
    "\141\142\143\144"
    # in hex codes
    "\x61\x62\x63\x64"
    # in unicodes
     "\u61\u62\u63\u64"
    # or
    "\U61\U62\U63\U64" 
```
We can also mix characters with octal/hex/unicode and use some oct codes and some hex codes together, as long as unicode characters are not mixed with octal/hex e.g. :
```
    # Valid
    "a\142\x63\x64"
    # Valid
    "ab\u63\U64"
    # Error: mixing Unicode and octal/hex escapes in a string is not allowed
    "\141\142\x63\u64"
```
See [the end of this section](https://cran.r-project.org/doc/manuals/r-devel/R-lang.html#Literal-constants) for further details.
2. Since functions can be written using string literals, e.g. `cat()` can be written alternatively :
```
'cat'()
"cat"()
`cat`()
```
we can use octal codes, hex codes and unicode for function names as well :
```
# all equal to cat()
"\143\141\164"()
`\x63\x61\x74`()
'\u63\u61\u74'()
"ca\u74"()
```
with the only exception that unicode sequences are not supported inside backticks ``
3. Round brackets can be avoided abusing operators e.g. :
```
cat('hello')
# can be written as
`+`=cat;+'hello'
```
An application of all the three tricks can be found [in this answer](https://codegolf.stackexchange.com/questions/165518/code-generator-with-unique-characters/165548#165548)
[Answer]
# Searching datasets
R's builtin datasets can be a trove, in particular for [restricted-source](/questions/tagged/restricted-source "show questions tagged 'restricted-source'") challenges, but searching through them is not easy.
For instance, for [this answer](https://codegolf.stackexchange.com/a/219617/86301), creating the value `100` thanks to a dataset was useful. It could be that the value was there as is, or as the sum of a column, or as the size of the dataset.
Here is the code I used; credit goes to Giuseppe [in the chat](https://chat.stackexchange.com/transcript/message/56532686#56532686):
```
x = data(package = "datasets")
res = x$results
for(dataset_name in paste0(res[,1],"::",res[,3])){
  curr = try(eval(parse(t=dataset_name)), T)
  if(length(curr) == 100) print(c("Length found in", dataset_name))
  if(100 %in% unlist(curr)) print(c("Value found in", dataset_name))
  try(if(try(sum(curr),T) == 100) print("Sum found in", dataset_name), T)
}
```
This searches all the datasets which are included by default; you can search datasets in other packages by changing the first line to `x <- data(package = .packages(all.available = TRUE))`.
You will need to make lavish use of `try` to avoid issues with different types of data. Remember that some datasets are vectors and not matrices or data frames. Other functions to try instead of `sum` include `min`, `max`, `mean`, `prod`, `sd` and `var`.
[Answer]
## `all` and `any` on lists
[Apparently](https://codegolf.stackexchange.com/a/241207/55372), `all` and `any` both accept lists of depth 1 and work just as well as on vectors (with a warning).
So, if you used `all(sapply(list,function))` in your code, in most places you may replace it with `all(Map(function,list))` for -3 bytes.
*I didn't know that before the [answer](https://codegolf.stackexchange.com/a/241207/55372) (linked above) by [@Dominic van Essen](https://codegolf.stackexchange.com/users/95126/dominic-van-essen).*
[Answer]
## Know your `apply` functions
1. As [already mentioned](https://codegolf.stackexchange.com/a/91294/55372), use `Map` instead of `mapply` (and possibly `lapply`, depending on whether you want vectorisation also on further arguments to the function).
2. Use `sapply` when you want the result simplified when possible.
3. Use `apply` on matrices to apply a function rowwise or columnwise. But remember of functions like `colSums` and `colMeans`.
4. Use `tapply(x,y,f)` instead of `sapply(split(x,y),f)` or `Map(f,split(x,y))` - but you may want to use `simplify=FALSE` in the second case sometimes.
Remember that sometimes the function you're using can apply another function to the results (through the `FUN` argument), e.g. `combn`.
[Answer]
# Numeric Tricks
Ignoring precision error:
* `x%/%1` instead of `floor(x)` (integer part for positive x)
* `x%%1` instead of `x-floor(x)` (fractional part for positive x)
* `!x%%1`, `x%%1` to check if integer or not
* `x^.5` instead of `sqrt(x)`
* `gamma(x+1)` instead of `factorial(x)`
Constants: \$\pi\$ `pi`, \$e\$ `exp(1)`, \$1/e\$ `exp(-1)`
(if you need these for some reason) \$1 / \sqrt{2\pi}\$ `dnorm(0)`, \$\gamma\$ `-digamma(1)`
]