Title: | Infix Functions For Vector Operations |
---|---|
Description: | Vector operations between grapes: An infix-only package! The 'invctr' functions perform common and less common operations on vectors, data frames matrices and list objects: - Extracting a value (range), or, finding the indices of a value (range). - Trimming, or padding a vector with a value of your choice. - Simple polynomial regression. - Set and membership operations. - General check & replace function for NAs, Inf and other values. |
Authors: | Fred Hasselman [aut, cre] |
Maintainer: | Fred Hasselman <[email protected]> |
License: | GPL-3 |
Version: | 0.2.0 |
Built: | 2025-02-18 16:01:27 UTC |
Source: | https://github.com/fredhasselman/invctr |
When your functions wear these rose tinted glasses, the world will appear to be a nicer, fluffier place.
x %00% y
x %00% y
x |
If (an element of) |
y |
The value to return/replace for |
Fred Hasselman
purrrr::%||%
Inf %00% NA numeric(0) %00% '' NA %00% 0 NaN %00% NA c(1, NaN) %00% NA NULL %00% NA c(1, NULL) %00% NA # can't see second element
Inf %00% NA numeric(0) %00% '' NA %00% 0 NaN %00% NA c(1, NaN) %00% NA NULL %00% NA c(1, NULL) %00% NA # can't see second element
Counters
Signed increment %+-%
Non-negative increment %++%
counter %+-% increment counter %++% increment
counter %+-% increment counter %++% increment
counter |
If |
increment |
An integer value |
## Not run: # Signed increment # Notice the difference between passing an object and a value for counter # Value (10 %+-% -5) (10 %+-% -5) # Object i <- 10 (i %+-% -5) (i %+-% -5) # This means we can use the infix in a while ... statement # WARNING: As is the case for any while ... statement, be careful not to create an infinite loop! i <- 10 while(i > -5){ i %+-% -5 print(i) } # Non-negative increment # Notice the difference between passing an object and a value for counter # Value (0 %++% 5) (0 %++% 5) # Object i <- 0 (i %++% 5) (i %++% 5) # This means we can use the infix in a while ... statement # WARNING: As is the case for any while ... statement, be careful not to create an infinite loop! i <- 0 while(i < 20){ i %++% 5 print(i) } ## End(Not run)
## Not run: # Signed increment # Notice the difference between passing an object and a value for counter # Value (10 %+-% -5) (10 %+-% -5) # Object i <- 10 (i %+-% -5) (i %+-% -5) # This means we can use the infix in a while ... statement # WARNING: As is the case for any while ... statement, be careful not to create an infinite loop! i <- 10 while(i > -5){ i %+-% -5 print(i) } # Non-negative increment # Notice the difference between passing an object and a value for counter # Value (0 %++% 5) (0 %++% 5) # Object i <- 0 (i %++% 5) (i %++% 5) # This means we can use the infix in a while ... statement # WARNING: As is the case for any while ... statement, be careful not to create an infinite loop! i <- 0 while(i < 20){ i %++% 5 print(i) } ## End(Not run)
Extract vectors by index or value occurrence
Extract front by first occurrence of a value %[f%
Extract front by last occurrence of a value %[l%
Extract vector front by index %[%
Extract vector rear by index %]%
Extract first occurrence of a value to vector rear %f]%
Extract last occurrence of a value to vector rear %l]%
Extract values at percentile and higher %q]%
Extract values higher than percentile %q)%
Extract values at percentile and smaller %[q%
Extract values smaller than percentile %(q%
Extract first, middle or last value of vector %:%
x %[f% v x %[l% v x %[% i x %]% i x %f]% v x %l]% v x %q]% q x %q)% q x %[q% q x %(q% q x %:% j
x %[f% v x %[l% v x %[% i x %]% i x %f]% v x %l]% v x %q]% q x %q)% q x %[q% q x %(q% q x %:% j
x |
A vector |
v |
A value of which the first or last occurrence in |
i |
An index or two element vector |
q |
A percentile value (between |
j |
A character indicating to extract the first |
A vector extracted from the front, rear, or, range of x
. Either based on an index or the first or last occurrence of a value or the first, middle, or, last element of a vector.
The function provided for symmetry, character lengths of x%]%i
and x[1:i]
are equal.
z <- letters # Extract front by first occurrence of value z %[f% "n" # Extract front by index x <- rnorm(100) x %[% 10 # Extract rear by index x %]% 90 # Extract rear by index x %]% 90 # Extract by indices if a range is provided x %]% c(4,30) z %[% c(6,10) # Extract last/middle value of x x %:% "l" z %:% "m" # Extract by percentile seq(1,10,.5) %(q% .5 # infix seq(1,10,.5)[seq(1,10,.5) < quantile(seq(1,10,.5),.5)] # regular syntax seq(1,10,.5) %q]% .5 # infix seq(1,10,.5)[seq(1,10,.5) >= quantile(seq(1,10,.5),.5)] # regular syntax
z <- letters # Extract front by first occurrence of value z %[f% "n" # Extract front by index x <- rnorm(100) x %[% 10 # Extract rear by index x %]% 90 # Extract rear by index x %]% 90 # Extract by indices if a range is provided x %]% c(4,30) z %[% c(6,10) # Extract last/middle value of x x %:% "l" z %:% "m" # Extract by percentile seq(1,10,.5) %(q% .5 # infix seq(1,10,.5)[seq(1,10,.5) < quantile(seq(1,10,.5),.5)] # regular syntax seq(1,10,.5) %q]% .5 # infix seq(1,10,.5)[seq(1,10,.5) >= quantile(seq(1,10,.5),.5)] # regular syntax
Find row or column by name or index
Column by name or index: %ci%
Row by name or number: %ri%
Matrix cell index by name or number: %mi%
Return all indices of a (range of) values: %ai%
Is element of... with multiple input types: %e%
c %ci% d r %ri% d rc %mi% d nv %ai% d x %e% y
c %ci% d r %ri% d rc %mi% d nv %ai% d x %e% y
c |
Column name or index |
d |
A named vector, list, matrix, or data frame |
r |
Row name or index |
rc |
A 2-element numeric or character vector representing |
nv |
A numeric value, or vector of values of which you want to know the indices in |
x |
A vector, data frame or list containing numbers and/or characters that could be elements of y |
y |
An object that could contain values in x |
If r/c/rc
is numeric, the name corresponding to the row/column index of d
, if r/c/rc
is a character vector, the row/column index corresponding to the row/column name. If dimnames(d) == NULL
, but names(d) != NULL
then %ci%
and %ri%
will look up r/c
in names(d)
Logical vector indicating which x are an element of y
Fred Hasselman
# data frame d <- data.frame(x=1:5,y=6,row.names=paste0("ri",5:1)) "y" %ci% d # y is the 2nd column of d 2 %ci% d # the name of the second column of d is "y" 2 %ri% d "ri5" %ri% d # change column name colnames(d)["y" %ci% d] <- "Yhat" # mi works on data frames, matrices, tiblles, etc. c(5,2) %mi% d list(r="ri1",c=2) %mi% d # matrix row and column indices m <- matrix(1:10,ncol=2, dimnames = list(paste0("ri",0:4),c("xx","yy"))) 1 %ci% m 5 %ci% m # no column 5 1 %ri% m 5 %ri% m c(5,1)%mi%m c(1,5)%mi%m # For list and vector objects ri and ci return the same values l <- list(a=1:100,b=LETTERS) 2 %ci% l "a" %ci% l 2 %ri% l "a" %ri% l # named vector v <- c("first" = 1, "2nd" = 1000) "2nd" %ci% v 1 %ci% v "2nd" %ri% v 1 %ri% v # get all indices of the number 1 in v 1 %ai% v # get all indices of the number 3 and 6 in d c(3,6) %ai% d # get all indices of values: Z < -1.96 and Z > 1.96 Z <- rnorm(100) Z[Z%)(%c(-1.96,1.96)] %ai% Z
# data frame d <- data.frame(x=1:5,y=6,row.names=paste0("ri",5:1)) "y" %ci% d # y is the 2nd column of d 2 %ci% d # the name of the second column of d is "y" 2 %ri% d "ri5" %ri% d # change column name colnames(d)["y" %ci% d] <- "Yhat" # mi works on data frames, matrices, tiblles, etc. c(5,2) %mi% d list(r="ri1",c=2) %mi% d # matrix row and column indices m <- matrix(1:10,ncol=2, dimnames = list(paste0("ri",0:4),c("xx","yy"))) 1 %ci% m 5 %ci% m # no column 5 1 %ri% m 5 %ri% m c(5,1)%mi%m c(1,5)%mi%m # For list and vector objects ri and ci return the same values l <- list(a=1:100,b=LETTERS) 2 %ci% l "a" %ci% l 2 %ri% l "a" %ri% l # named vector v <- c("first" = 1, "2nd" = 1000) "2nd" %ci% v 1 %ci% v "2nd" %ri% v 1 %ri% v # get all indices of the number 1 in v 1 %ai% v # get all indices of the number 3 and 6 in d c(3,6) %ai% d # get all indices of values: Z < -1.96 and Z > 1.96 Z <- rnorm(100) Z[Z%)(%c(-1.96,1.96)] %ai% Z
Values inside interval
In closed interval: %[]%
In open interval: %()%
In half-closed interval (left): %[)%
In half-closed interval (right): %(]%
Return x in closed interval: %[.]%
Return x in open interval: %(.)%
Return x in half-closed interval (left): %[.)%
Return x in half-closed interval (right): %(.]%
x %[]% j x %()% j x %[)% j x %(]% j x %[.]% j x %(.)% j x %[.)% j x %(.]% j
x %[]% j x %()% j x %[)% j x %(]% j x %[.]% j x %(.)% j x %[.)% j x %(.]% j
x |
A vector |
j |
A 2-element numeric vector indicating a range |
Logical vector of length x
, or, values in the range j
Package DescTools
provides similar functions
# Closed interval 0:5 %[]% c(1,5) # logical vector 0:5 %[.]% c(1,5) # extract values # Open interval 0:5 %()% c(1,5) 0:5 %(.)% c(1,5) # Closed interval left 0:5 %[)% c(1,5) 0:5 %[.)% c(1,5) # Closed interval right 0:5 %(]% c(1,5) 0:5 %(.]% c(1,5)
# Closed interval 0:5 %[]% c(1,5) # logical vector 0:5 %[.]% c(1,5) # extract values # Open interval 0:5 %()% c(1,5) 0:5 %(.)% c(1,5) # Closed interval left 0:5 %[)% c(1,5) 0:5 %[.)% c(1,5) # Closed interval right 0:5 %(]% c(1,5) 0:5 %(.]% c(1,5)
Values outside interval
Not in closed interval: %][%
Not in open interval: %)(%
Not in half-closed interval (left): %](%
Not in half-closed interval (right): %)[%
Return x not in closed interval: %].[%
Return x not in open interval: %).(%
Return x not in half-closed interval (left): %].(%
Return x not in half-closed interval (right): %).[%
x %][% j x %)(% j x %](% j x %)[% j x %].[% j x %).(% j x %].(% j x %).[% j
x %][% j x %)(% j x %](% j x %)[% j x %].[% j x %).(% j x %].(% j x %).[% j
x |
A vector |
j |
A range |
logical vector of length x, or, values of x outside the range j
Package DescTools
provides similar functions
# Closed interval 5%][%c(1,5) 5%].[%c(1,5) # Open interval 5%)(%c(1,5) 5%).(%c(1,5) # Half-losed interval left 5%](%c(1,5) 5%].(%c(1,5) # Half-losed interval right 5%)[%c(1,5) 5%).[%c(1,5)
# Closed interval 5%][%c(1,5) 5%].[%c(1,5) # Open interval 5%)(%c(1,5) 5%).(%c(1,5) # Half-losed interval left 5%](%c(1,5) 5%].(%c(1,5) # Half-losed interval right 5%)[%c(1,5) 5%).[%c(1,5)
Padd vector by index
Pad vector front %[+%
Pad vector rear %+]%
Pad vector front + rear %[+]%
x %[+% j x %+]% j x %[+]% j
x %[+% j x %+]% j x %[+]% j
x |
A vector |
j |
A one, or two element vector. One element: Pad |
A padded version of x
x <- rnorm(100) # Pad front with 10 zeros x%[+%10 # Same as x%[+%c(10,0) # Pad rear with zeros x%+]%10 # Same as x%+]%c(10,0) # Pad front + rear with NA x%[+]%c(NA,10) # Pad front + rear of a character vector "yes"%[+]%c(2,"no") "yes"%[+]%c(1,"no") "yes"%[+]%c(0,"no")
x <- rnorm(100) # Pad front with 10 zeros x%[+%10 # Same as x%[+%c(10,0) # Pad rear with zeros x%+]%10 # Same as x%+]%c(10,0) # Pad front + rear with NA x%[+]%c(NA,10) # Pad front + rear of a character vector "yes"%[+]%c(2,"no") "yes"%[+]%c(1,"no") "yes"%[+]%c(0,"no")
Regress vectors
Correlate x and y: %/r%
Polynomial regression of degree 1: %/1%
Polynomial regression of degree 2: %/2%
Polynomial regression of degree 3: %/3%
Polynomial regression of degree 4: %/4%
%/n%
Polynomial regression of degree n: %/n%
x %/r% y x %/1% y x %/2% y x %/3% y x %/4% y x %/n% yn
x %/r% y x %/1% y x %/2% y x %/3% y x %/4% y x %/n% yn
x |
Numeric vectors |
y |
Numeric vector |
yn |
List of length 2, first element is a vector |
x <- rnorm(100) y <- x + x^2 + x^3 # Correlate x with y x%/r%y # Polynomial regression degree 1 .. 4 x%/1%y x%/2%y x%/3%y x%/4%y anova(x%/1%y,x%/2%y,x%/3%y,x%/4%y) # Order n x%/n%list(y,10)
x <- rnorm(100) y <- x + x^2 + x^3 # Correlate x with y x%/r%y # Polynomial regression degree 1 .. 4 x%/1%y x%/2%y x%/3%y x%/4%y anova(x%/1%y,x%/2%y,x%/3%y,x%/4%y) # Order n x%/n%list(y,10)
Trim vector by index
Trim vector front %[-%
Trim vector rear %-]%
Trim vector front + rear %[-]%
x %[-% i x %-]% i x %[-]% j
x %[-% i x %-]% i x %[-]% j
x |
A vector |
i |
A 1 element vector by which the rear of |
j |
A one, or two element numeric vector. One element: Trim front by |
A trimmed version of x
x <- rnorm(100) # Trim front x%[-%5 # Trim rear x%-]%5 # Trim front + rear x%[-]%c(2,10) x%[-]%7
x <- rnorm(100) # Trim front x%[-%5 # Trim rear x%-]%5 # Trim front + rear x%[-]%c(2,10) x%[-]%7