The function groupsinsiders,outsiders andextractors provide infix functions that can be used toextract values from vectors.
insiders andoutsidersThese functions return values inside or outside a given interval.Inclusion or exclusion of interval endpoints follows the common notationfor open and closed intervals:[ and] meansinclusion, and( and) means exclusion ofendpoints.
The syntax is always:
vector infix interval
Depending on which function is called, the return value is either alogical vector indicating which values are inside or outside theinterval, or, the actual values (use the functions with a dot betweenthe operators%[.]%)
The syntax and function is similar to those provided in packageDescTools (I did not test whether they give the sameresults).
x<-0:9# Inside open intervalx%()%c(5,9)> [1]FALSEFALSEFALSEFALSEFALSEFALSETRUETRUETRUEFALSE# Inside closed intervalx%[]%c(5,9)> [1]FALSEFALSEFALSEFALSEFALSETRUETRUETRUETRUETRUE# Outside open intervalx%)(%c(5,9)> [1]TRUETRUETRUETRUETRUEFALSEFALSEFALSEFALSEFALSE# Outside closed intervalx%][%c(5,9)> [1]TRUETRUETRUETRUETRUETRUEFALSEFALSEFALSETRUE# All variations left/right open/closed are possiblex%[)%c(5,9)> [1]FALSEFALSEFALSEFALSEFALSETRUETRUETRUETRUEFALSEx%](%c(5,9)> [1]TRUETRUETRUETRUETRUETRUEFALSEFALSEFALSEFALSEIndices are commonly used to extract values, if you add a dot. inbetween the the interval symbols, values will beextracted.
# Regular indexing works, but is a bit 'wordy'x[x%[]%c(5,9)]> [1]56789# Easier to use the special functionsx%[.]%c(5,9)> [1]56789# Extract first, last, or, middle value of xx%:%"f"> [1]0x%:%"m"> [1]4x%:%"l"> [1]9# Simulate a sample from a standard normal distributionset.seed(4321)Zscore<-rnorm(100)# Find Z-scores that are 'significant' at alpha = .05Zscore%).(%c(-1.96,1.96)> [1]2.080248-2.450016-2.439320# Old indexing has a lot of repetition, so does tidyverse, e.g. using filter()Zscore[Zscore<-1.96| Zscore>1.96]> [1]2.080248-2.450016-2.439320extractorsExtracting a subset of values from the front or rear of a vector is acommon task and thebase functionshead() andtail() can do this. The infix functions in theextractors group mimic some of this behaviour and add theability to extractfrom - to, or,up -and-untill, aspecific value.
# A character vectorz<- letters# Extract front by first occurrence of value "n"z%[f%"n"> [1]"a""b""c""d""e""f""g""h""i""j""k""l""m""n"# Extact first, middle, last of zz%:%"f"> [1]"a"z%:%"m"> [1]"m"z%:%"l"> [1]"z"# Extract by percentileseq(1,10,.5)%(q% .5# infix> [1]1.01.52.02.53.03.54.04.55.0seq(1,10,.5)[seq(1,10,.5)<quantile(seq(1,10,.5),.5)]# regular syntax> [1]1.01.52.02.53.03.54.04.55.0seq(1,10,.5)%q]% .5# infix> [1]5.56.06.57.07.58.08.59.09.510.0seq(1,10,.5)[seq(1,10,.5)>=quantile(seq(1,10,.5),.5)]# regular syntax> [1]5.56.06.57.07.58.08.59.09.510.0# Random uniform integersset.seed(123)x<-round(runif(100,1,100))# Extract front up and untill index 10x%[%10# infix> [1]2979418894653895646x[1:10]# regular [saves just 1 char]> [1]2979418894653895646# Extract from index 90 to rearx%]%90# infix> [1]1814663566332078104752x[90:length(x)]# regular> [1]1814663566332078104752# Extract numbers from front to first occurrence of 11x%[f%11# infix> [1]29794188946538956469646685811x[1:which(x==11)[1]]# regular> [1]29794188946538956469646685811# Extract numbers from last occurrence of 11 to rearx%l]%11# infix> [1]11449989891814663566332078104752x[which(x==11)[length(which(x==11))]:length(x)]# regular> [1]11449989891814663566332078104752# Extract by indices if an index range provided# This is a clear case in which the infix is less sensible to use than regular indexing:x%]%c(6,10)# infix> [1]653895646x[6:10]# regular> [1]653895646z%[%c(6,10)#infix> [1]"f""g""h""i""j"z[6:10]#regular> [1]"f""g""h""i""j"ThefINDexers group provides infix functions that canreturn column and row names based on indices, or, indices based oncolumn and row names. Take for instance data framed:
| x | y | txt | |
|---|---|---|---|
| ri5 | 1 | 6 | delta = 5 |
| ri4 | 2 | 6 | delta = 4 |
| ri3 | 3 | 6 | delta = 3 |
| ri2 | 4 | 6 | delta = 2 |
| ri1 | 5 | 6 | delta = 1 |
We can use the infix functions to get names and indices ofd:
# Columns"txt"%ci%d# infix> [1]3which(colnames(d)%in%"txt")# regular> [1]32%ci%d# infix> [1]"y"colnames(d)[2]# regular> [1]"y"# Rows"ri4"%ri%d# infix> [1]2which(rownames(d)%in%"ri4")# regular> [1]22%ri%d# infix> [1]"ri4"rownames(d)[2]# regular> [1]"ri4"# Change column namecolnames(d)["y"%ci%d]<-"Yhat"# infixcolnames(d)[colnames(d)%in%"y"]<-"Yhat"# regularFor 1D list and vector objects%ri% and%ci% return the same value.
l<-list(a=1:100,b=LETTERS)2%ci%l==2%ri%l> [1]TRUE"a"%ci%l=="a"%ri%l> [1]TRUE# Named vector v<-c("first"=1,"2nd"=1000)1%ci%v==1%ri%v> [1]TRUE"2nd"%ci%v=="2nd"%ri%v> [1]TRUEFunction%mi% will return row and/or column names on 2Dobjects: data frames, matrices, tibbles, etc.
# Data frame dc(5,2)%mi% d> [1]"ri1""Yhat"list(r="ri1",c=2)%mi% d>$r> [1]5>>$c> [1]"Yhat"# matrix row and column indices(m<-matrix(1:10,ncol=2,dimnames =list(paste0("ri",0:4),c("xx","yy"))))> xx yy> ri016> ri127> ri238> ri349> ri45101%ci% m> [1]"xx"5%ci% m# no column 5> [1]NA1%ri% m> [1]"ri0"5%ri% m> [1]"ri4"c(5,1)%mi%m> [1]"ri4""xx"c(1,5)%mi%m> [1]"ri0"NAFunction%ai% is a version of%in% thatreturns the indices of all occurrences of one or more values in anobject.
# get all indices of the number 1 in v1%ai% v> nv first>111# get all indices of the number 3 and 6 in dc(3,6)%ai% d> nv row col>1331>2612>3622>4632>5642>6652# Simulate a sample from a standard normal distributionset.seed(1234) Zscores<-rnorm(100) Zscores%).(%c(-1.96,1.96)%ai% Zscores# returns a data frame with values and indices> nv V1>1-2.345697702629354>22.4158351784893420>3-2.1800396489486737>42.5489910707178662>52.0702708613309475>62.12111710537568100which(Zscores%)(%c(-1.96,1.96))# returns an index vector> [1]420376275100