Skip to content

Instantly share code, notes, and snippets.

@johnmyleswhite
Created September 18, 2014 05:32
Show Gist options
  • Save johnmyleswhite/3dc3108d2b4b527a8468 to your computer and use it in GitHub Desktop.
Save johnmyleswhite/3dc3108d2b4b527a8468 to your computer and use it in GitHub Desktop.
R indexing showcases all possible design theories
> v <- c(1, 2, 3, 4)
> v[1]
[1] 1
> v[4]
[1] 4
> v[0]
numeric(0)
> v[5]
[1] NA
> v[-1]
[1] 2 3 4
> v[NA]
[1] NA NA NA NA
> v[-NA]
[1] NA
> v[NULL]
numeric(0)
> v[-NULL]
Error in -NULL : invalid argument to unary operator
@shoestringpsycholing
Copy link

v[-5]
[1] 1 2 3 4
v[-0]
integer(0)

@jmcastagnetto
Copy link

> v[Inf]
[1] NA
> v[-Inf]
[1] NA
> v[NaN]
[1] NA
> v[v]
[1] 1 2 3 4
> v[2*v]
[1]  2  4 NA NA
> v[-v]
numeric(0)

# same as v[3]
> v[pi]
[1] 3
> pi
[1] 3.141593

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment