Title: | Additional Operators to Help you Write Cleaner R Code |
---|---|
Description: | Provides string arithmetic, reassignment operators, logical operators that handle missing values, and extra logical operators such as floating point equality and all or nothing. The intent is to allow R users to write code that is easier to read, write, and maintain while providing a friendlier experience to new R users from other language backgrounds (such as 'Python') who are used to concepts such as x += 1 and 'foo' + 'bar'. Includes operators for not in, easy floating point comparisons, === equivalent, and SQL-like like operations (), etc. We also added in some extra helper functions, such as OS checks, pasting in Oxford comma format, and functions to get the first, last, nth, or most common element of a vector or word in a string. |
Authors: | Ben Wiseman [cre, aut, ccp], Steven Nydick [aut, ccp]
|
Maintainer: | Ben Wiseman <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.3.14 |
Built: | 2025-01-30 02:05:33 UTC |
Source: | https://github.com/benwiseman/roperators |
%na<-%
is a simple shortcut to assign a specific value to all
NA elements contained in x.
x %na<-% value
x %na<-% value
x |
a vector |
value |
value to replace vector's missing values with |
Ben Wiseman, [email protected]
x <- c("a", NA, "c") x %na<-% "b" print(x) # "a" "b" "c" x <- c(1, NA, 3, NA) x %na<-% c(2,4) print(x) # 1 2 3 4
x <- c("a", NA, "c") x %na<-% "b" print(x) # "a" "b" "c" x <- c(1, NA, 3, NA) x %na<-% c(2,4) print(x) # 1 2 3 4
This takes two arguments just like gsub
- a patterns and a replacement.
It will totally overwrite any element where the pattern is matched with the second.
If you want to simply apply a regex (i.e. replace only the specific bit that matches),
use %regex=%
instead. If you want to replace with nothing (""), just just %-%
or
%-=% instead
.
x %regex<-% value
x %regex<-% value
x |
a character vector |
value |
c(pattern, replacement) |
Ben Wiseman, [email protected]
# Overwrite elements that match regex: x <- c("a1b", "b1", "c", "d0") # overwrite any element containing a number x %regex<-% c("\\d+", "x") print(x) # "x" "b" "c" "x"
# Overwrite elements that match regex: x <- c("a1b", "b1", "c", "d0") # overwrite any element containing a number x %regex<-% c("\\d+", "x") print(x) # "x" "b" "c" "x"
This takes two arguments just like gsub
- a patterns and a replacement.
It will only overwrite the parts of any character where the pattern is matched with the second argument.
If you want to overwrite whole elements via a regex (i.e. replace the entire element if it matches),
use %regex<-%
instead.
x %regex=% value
x %regex=% value
x |
a character vector |
value |
c(pattern, replacement) |
Ben Wiseman, [email protected]
# Apply a regular expression/substitution to x: x <- c("a1b", "b1", "c", "d0") # change any number to "x" x %regex=% c("\\d+", "x") print(x) # "axb" "b" "c" "dx"
# Apply a regular expression/substitution to x: x <- c("a1b", "b1", "c", "d0") # change any number to "x" x %regex=% c("\\d+", "x") print(x) # "axb" "b" "c" "dx"
Modifies the stored value of the left-hand-side object by the right-hand-side object.
Equivalent of operators such as +=
-=
*=
/=
in languages like c++ or python.
%+=%
and %-=%
can also work with strings.
x %+=% y x %-=% y x %*=% y x %/=% y x %^=% y x %log=% y x %root=% y
x %+=% y x %-=% y x %*=% y x %/=% y x %^=% y x %log=% y x %root=% y
x |
a stored value |
y |
value to modify stored value by |
Ben Wiseman, [email protected]
x <- 1 x %+=% 2 x == 3 # TRUE x %-=% 3 x == 0 # TRUE # Or with data frames... test <- iris # Simply modify in-place test$Sepal.Length[test$Species == 'setosa' & test$Petal.Length < 1.5] %+=% 1 # Which is much nicer than typing: test$Sepal.Length[test$Species == 'setosa' & test$Petal.Length < 1.5] <- test$Sepal.Length[test$Species == 'setosa' & test$Petal.Length < 1.5] + 1 # ...which is over the 100 character limit for R doccumentation! # %+=% and %-=% also work with strings x <- "ab" x %+=% "c" x %-=% "b" x == "ac" # TRUE # %-=% can also take regular expressions x <- "foobar" x %-=% "[f|b]" print(x) # "ooar"
x <- 1 x %+=% 2 x == 3 # TRUE x %-=% 3 x == 0 # TRUE # Or with data frames... test <- iris # Simply modify in-place test$Sepal.Length[test$Species == 'setosa' & test$Petal.Length < 1.5] %+=% 1 # Which is much nicer than typing: test$Sepal.Length[test$Species == 'setosa' & test$Petal.Length < 1.5] <- test$Sepal.Length[test$Species == 'setosa' & test$Petal.Length < 1.5] + 1 # ...which is over the 100 character limit for R doccumentation! # %+=% and %-=% also work with strings x <- "ab" x %+=% "c" x %-=% "b" x == "ac" # TRUE # %-=% can also take regular expressions x <- "foobar" x %-=% "[f|b]" print(x) # "ooar"
Shorthand for some common mathematical operators
n %C% k n %P% k
n %C% k n %P% k
n |
whole number (from n choose/permute k) |
k |
whole number (from n choose/permute k) |
Ben Wiseman, [email protected]
# Calculate 5 choose 3 print(5 %C% 3) # Calculate 5 permute 3 print(5 %P% 3)
# Calculate 5 choose 3 print(5 %C% 3) # Calculate 5 permute 3 print(5 %P% 3)
Cleaner conversion functions
convert x to arbitrary class
chr(x, ...) int(x, ...) dbl(x, ...) num(x, ...) bool(x, ...) as.class(x, class)
chr(x, ...) int(x, ...) dbl(x, ...) num(x, ...) bool(x, ...) as.class(x, class)
x |
object to be converted |
... |
other args for as. conversion |
class |
chatracter name of the class to convert x to |
These are shorthand aliases for common conversions There is nothing magical here, but it can make your code more readable
Steven Nydick, [email protected]
Ben Wiseman, [email protected]
chr(42) # "42" = as.character int(42.1) # 42L = as.integer dbl("42L") # 42.0 = as.double num("42") # 42 = as.numeric bool(42) # TRUE = as.logical foo <- 255 as.class(foo, "roman") # [1] CCLV
chr(42) # "42" = as.character int(42.1) # 42L = as.integer dbl("42L") # 42.0 = as.double num("42") # 42 = as.numeric bool(42) # TRUE = as.logical foo <- 255 as.class(foo, "roman") # [1] CCLV
These operators introduce improved NA handling, reliable floating point tests, and intervals. Specifically:
Equality that handles missing values
Floating point equality, an important bit of functionality missing in base R (%~=%
)
Strict (value and type) equality, for those familiar with Javascript ===
Greater/less than or equal to with missing value equality
Greater/less than or equal to with floating point and missing equality
Between (ends excluded)
Between (ends included)
x %==% y x %===% y x %>=% y x %<=% y x %><% y x %>=<% y
x %==% y x %===% y x %>=% y x %<=% y x %><% y x %>=<% y
x |
a vector |
y |
a vector |
Ben Wiseman, [email protected]
Other comparisons:
floating_point_comparisons
## Greater/Less than | Equal c(1, NA, 3, 4) == c(1, NA, 4, 3) # TRUE NA FALSE FALSE c(1, NA, 3, 4) %==% c(1, NA, 4, 3) # TRUE TRUE FALSE FALSE c(1, NA, 3, 4) %>=% c(1, NA, 4, 3) # TRUE TRUE FALSE TRUE c(1, NA, 3, 4) %<=% c(1, NA, 4, 3) # TRUE TRUE TRUE FALSE # Strict equality - a la javascript's === # Only tru if the class and value of x and y are the same x <- int(2) y <- 2 x == y # TRUE x %===% y # FALSE x %===% int(y) # TRUE # NOTE parentheses surrounding expression before this operator are necessary # Without parentheses it would be interpreted as .1 + .1 + (.1 %~=% .3) #### Between #### # ends excluded 2 %><% c(1, 3) # TRUE 3 %><% c(1, 3) # FALSE # ends included 2 %>=<% c(1, 3) # TRUE 3 %>=<% c(1, 3) # TRUE
## Greater/Less than | Equal c(1, NA, 3, 4) == c(1, NA, 4, 3) # TRUE NA FALSE FALSE c(1, NA, 3, 4) %==% c(1, NA, 4, 3) # TRUE TRUE FALSE FALSE c(1, NA, 3, 4) %>=% c(1, NA, 4, 3) # TRUE TRUE FALSE TRUE c(1, NA, 3, 4) %<=% c(1, NA, 4, 3) # TRUE TRUE TRUE FALSE # Strict equality - a la javascript's === # Only tru if the class and value of x and y are the same x <- int(2) y <- 2 x == y # TRUE x %===% y # FALSE x %===% int(y) # TRUE # NOTE parentheses surrounding expression before this operator are necessary # Without parentheses it would be interpreted as .1 + .1 + (.1 %~=% .3) #### Between #### # ends excluded 2 %><% c(1, 3) # TRUE 3 %><% c(1, 3) # FALSE # ends included 2 %>=<% c(1, 3) # TRUE 3 %>=<% c(1, 3) # TRUE
Univariate and bivariate summaries and statistics with the least missing data removed (such as complete-cases correlations). These are typically default arguments to standard statistics functions.
length_cc(x, ...) n_unique_cc(x, ...) min_cc(x, ...) max_cc(x, ...) range_cc(x, ...) all_cc(x, ...) any_cc(x, ...) sum_cc(x, ...) prod_cc(x, ...) mean_cc(x, ...) median_cc(x, ...) var_cc(x, y = NULL, ...) cov_cc(x, y = NULL, ...) cor_cc(x, y = NULL, ...) sd_cc(x, ...) weighted.mean_cc(x, w, ...) quantile_cc(x, ...) IQR_cc(x, ...) mad_cc(x, ...) rowSums_cc(x, ...) colSums_cc(x, ...) rowMeans_cc(x, ..., rescale = FALSE) colMeans_cc(x, ..., rescale = FALSE)
length_cc(x, ...) n_unique_cc(x, ...) min_cc(x, ...) max_cc(x, ...) range_cc(x, ...) all_cc(x, ...) any_cc(x, ...) sum_cc(x, ...) prod_cc(x, ...) mean_cc(x, ...) median_cc(x, ...) var_cc(x, y = NULL, ...) cov_cc(x, y = NULL, ...) cor_cc(x, y = NULL, ...) sd_cc(x, ...) weighted.mean_cc(x, w, ...) quantile_cc(x, ...) IQR_cc(x, ...) mad_cc(x, ...) rowSums_cc(x, ...) colSums_cc(x, ...) rowMeans_cc(x, ..., rescale = FALSE) colMeans_cc(x, ..., rescale = FALSE)
x |
An R object. Currently there are methods for
numeric/logical vectors and date,
date-time and time interval objects. Complex vectors
are allowed for |
... |
arguments to pass to wrapped functions |
y |
|
w |
a numerical vector of weights the same length as |
rescale |
whether to rescale the matrix/df/vector before calculating summaries |
n_o <- 20 n_m <- round(n_o / 3) x <- rnorm(n_o) y <- rnorm(n_o) x[sample(n_o, n_m)] <- NA y[sample(n_o, n_m)] <- NA mean_cc(x) # mean of complete cases mean_cc(y) var_cc(x) # variance of complete cases var_cc(y) cor_cc(x, y) # correlation between available cases
n_o <- 20 n_m <- round(n_o / 3) x <- rnorm(n_o) y <- rnorm(n_o) x[sample(n_o, n_m)] <- NA y[sample(n_o, n_m)] <- NA mean_cc(x) # mean of complete cases mean_cc(y) var_cc(x) # variance of complete cases var_cc(y) cor_cc(x, y) # correlation between available cases
Misc/useful functions to easily determine what is contained in a vector.
is.constant(x) is.binary(x)
is.constant(x) is.binary(x)
x |
object to be tested |
a logical value
Convert factor with numeric labels into numeric vector
f.as.numeric(x)
f.as.numeric(x)
x |
a factor with numeric labels |
Ulrike Grömping, [email protected]
Ben Wiseman, [email protected]
x <- factor(c(11, 22, 33, 99)) as.numeric(x) # 1 2 3 4 # NOT typically the desired.expected output f.as.numeric(x) # 11 22 33 99 # Typically desired output # Or... as.numeric(as.character(x)) # A tad unsightly
x <- factor(c(11, 22, 33, 99)) as.numeric(x) # 1 2 3 4 # NOT typically the desired.expected output f.as.numeric(x) # 11 22 33 99 # Typically desired output # Or... as.numeric(as.character(x)) # A tad unsightly
Check whether file extension is as specified
is_txt_file(x) is_csv_file(x) is_excel_file(x) is_r_file(x) is_rdata_file(x) is_rda_file(x) is_rds_file(x) is_spss_file(x) check_ext_against(x, ext = "txt")
is_txt_file(x) is_csv_file(x) is_excel_file(x) is_r_file(x) is_rdata_file(x) is_rda_file(x) is_rds_file(x) is_spss_file(x) check_ext_against(x, ext = "txt")
x |
file(s) to be tested |
ext |
extension to test against |
a logical value
These only check the file extension and not the contents of the file. Checking
the contents of a file might come later but would be quite a bit more involved.
You can use readr
or readxl
(for example) to check the file contents.
# create your own file extension checks is_word_file <- function(x){ check_ext_against(x, ext = c("doc", "docx")) } is_word_file(c("blah.doc", "blah.docx", "blah.txt"))
# create your own file extension checks is_word_file <- function(x){ check_ext_against(x, ext = c("doc", "docx")) } is_word_file(c("blah.doc", "blah.docx", "blah.txt"))
These are an important set of operators missing from base R. In particular,
using ==
on two non-interger numbers can give unexpected results (see examples.)
See this for details: https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
x %~=% y x %>~% y x %<~% y
x %~=% y x %>~% y x %<~% y
x |
numeric |
y |
numeric |
Ben Wiseman, [email protected]
Other comparisons:
comparisons
## Floating point test of equality #### # Basic Equality - no roperators: (0.1 + 0.1 + 0.1) == 0.3 # FALSE # Basic Equality - with roperators: (0.1 + 0.1 + 0.1) %~=% 0.3 # TRUE # NOTE: for floating point >= and <= (0.1 + 0.1 + 0.1) %>=% 0.3 # TRUE (0.1 + 0.1 + 0.1) %<=% 0.3 # FALSE # Use >~ and <~ for greater/less than or approx equal (0.1 + 0.1 + 0.1) %>~% 0.3 # TRUE (0.1 + 0.1 + 0.1) %<~% 0.3 # TRUE
## Floating point test of equality #### # Basic Equality - no roperators: (0.1 + 0.1 + 0.1) == 0.3 # FALSE # Basic Equality - with roperators: (0.1 + 0.1 + 0.1) %~=% 0.3 # TRUE # NOTE: for floating point >= and <= (0.1 + 0.1 + 0.1) %>=% 0.3 # TRUE (0.1 + 0.1 + 0.1) %<=% 0.3 # FALSE # Use >~ and <~ for greater/less than or approx equal (0.1 + 0.1 + 0.1) %>~% 0.3 # TRUE (0.1 + 0.1 + 0.1) %<~% 0.3 # TRUE
Little functions to replace common minor functions. useful in apply sttements
Get most common thing(s)
Return number of unique things in x
Return vector of n points evenly spaced around the origin point
get_1st(x, type = "v") get_last(x, type = "v") get_nth(x, n = 1, type = "v") get_1st_word(x, type = "v", split = " ") get_last_word(x, type = "v", split = " ") get_nth_word(x, n = 1, type = "v", split = " ") get_most_frequent(x, collapse = NULL) get_most_frequent_word( x, ignore.punct = TRUE, ignore.case = TRUE, split = " ", collapse = NULL, punct.regex = "[[:punct:]]", punct.replace = "" ) n_unique(x, na.rm = FALSE) seq_around(origin = 1, n = 1, spacing = 0.25)
get_1st(x, type = "v") get_last(x, type = "v") get_nth(x, n = 1, type = "v") get_1st_word(x, type = "v", split = " ") get_last_word(x, type = "v", split = " ") get_nth_word(x, n = 1, type = "v", split = " ") get_most_frequent(x, collapse = NULL) get_most_frequent_word( x, ignore.punct = TRUE, ignore.case = TRUE, split = " ", collapse = NULL, punct.regex = "[[:punct:]]", punct.replace = "" ) n_unique(x, na.rm = FALSE) seq_around(origin = 1, n = 1, spacing = 0.25)
x |
vector |
type |
'v' (default) for vector |
n |
number of points to create |
split |
character that separated words. Default = ' ' |
collapse |
OPTIONAL character - paste output into single string with collapse |
ignore.punct |
logical - ignore punctuation marks |
ignore.case |
logical - ignore case (if true, will return in lower) |
punct.regex |
character - regex used to remove punctuation (by default |
punct.replace |
character - what to replace punctuation with (default is "") |
na.rm |
whether to ignore NAs when determining uniqueness |
origin |
number to center sequence around |
spacing |
distance between any two points in the sequence |
a vector of most common element(s). Will be character unless x is numeric and you don't tell it to collapse into a single string!
a vector of most common element(s). Will be character unless x is numeric and you don't tell it to collapse into a single string!
Numeric vector. Will default to 1 if arguments are left blank to conform with default seq() behaviour.
Ben Wiseman, [email protected]
# listr of car names car_names <- strsplit(row.names(mtcars)[1:5], " ") sapply(car_names, get_1st) # [1] "Mazda" "Mazda" "Datsun" "Hornet" "Hornet" sapply(car_names, get_nth, 2) # [1] "RX4" "RX4" "710" "4" "Sportabout" # OR if you just want to pull a simple string apart (e.g. someone's full name): get_1st_word(rownames(mtcars)[1:5]) #[1] "Mazda" "Mazda" "Datsun" "Hornet" "Hornet" get_last_word(rownames(mtcars)[1:5]) #[1] "RX4" "Wag" "710" "Drive" "Sportabout" get_nth_word(rownames(mtcars)[1:5], 2) #[1] "RX4" "RX4" "710" "4" "Sportabout" my_stuff <- c(1:10, 10, 5) # These are straight forward get_1st(my_stuff) get_nth(my_stuff, 3) get_last(my_stuff) get_most_frequent(my_stuff) my_chars <- c("a", "b", "b", "a", "g", "o", "l", "d") get_most_frequent(my_chars) get_most_frequent(my_chars, collapse = " & ") generic_string <- "Who's A good boy? Winston's a good boy!" get_1st_word(generic_string) get_nth_word(generic_string, 3) get_last_word(generic_string) # default ignores case and punctuation get_most_frequent_word(generic_string) # can change like so: get_most_frequent_word(generic_string, ignore.case = FALSE, ignore.punct = FALSE)
# listr of car names car_names <- strsplit(row.names(mtcars)[1:5], " ") sapply(car_names, get_1st) # [1] "Mazda" "Mazda" "Datsun" "Hornet" "Hornet" sapply(car_names, get_nth, 2) # [1] "RX4" "RX4" "710" "4" "Sportabout" # OR if you just want to pull a simple string apart (e.g. someone's full name): get_1st_word(rownames(mtcars)[1:5]) #[1] "Mazda" "Mazda" "Datsun" "Hornet" "Hornet" get_last_word(rownames(mtcars)[1:5]) #[1] "RX4" "Wag" "710" "Drive" "Sportabout" get_nth_word(rownames(mtcars)[1:5], 2) #[1] "RX4" "RX4" "710" "4" "Sportabout" my_stuff <- c(1:10, 10, 5) # These are straight forward get_1st(my_stuff) get_nth(my_stuff, 3) get_last(my_stuff) get_most_frequent(my_stuff) my_chars <- c("a", "b", "b", "a", "g", "o", "l", "d") get_most_frequent(my_chars) get_most_frequent(my_chars, collapse = " & ") generic_string <- "Who's A good boy? Winston's a good boy!" get_1st_word(generic_string) get_nth_word(generic_string, 3) get_last_word(generic_string) # default ignores case and punctuation get_most_frequent_word(generic_string) # can change like so: get_most_frequent_word(generic_string, ignore.case = FALSE, ignore.punct = FALSE)
inline call to integrate that returns integration value rather than list
f %integrate% range
f %integrate% range
f |
function (with numeric return) |
range |
vector of two numbers c(low, high) |
Ben Wiseman, [email protected]
f <- function(x) x^2 print(f %integrate% c(0, 1)) # vs base x <- integrate(f, 0, 1) str(x)
f <- function(x) x^2 print(f %integrate% c(0, 1)) # vs base x <- integrate(f, 0, 1) str(x)
loads package if available, else tries to install it (from CRAN by default)
loads package if available, else tries to install it (from CRAN by default)
library.force(pkg, ...) require.force(pkg, ...)
library.force(pkg, ...) require.force(pkg, ...)
pkg |
name of package to load/install |
... |
other args used by install.packages |
These are some convienience functions, such as a not-in, and xor operator.
This takes two arguments just like grepl
- a string and a pattern.
TRUE if grepl(pattern, x, ignore.case=TRUE)
would be TRUE
This takes two arguments just like grepl
- a string and a pattern.
TRUE if grepl(pattern, x, ignore.case=FALSE, perl=TRUE)
would be TRUE.
It's like %like%
from data.table (but slower, preferably use data.table).
x %ni% y x %xor% y x %aon% y x %rlike% pattern x %perl% pattern
x %ni% y x %xor% y x %aon% y x %rlike% pattern x %perl% pattern
x |
a character vector |
y |
a vector |
pattern |
a single character expression |
data.table has a %like% operator which you should try to use instead if working with data.table!
Ben Wiseman, [email protected]
#### Not in #### "z" %ni% c("a", "b", "c") # TRUE #### Exclusive or #### TRUE %xor% TRUE # FALSE FALSE %xor% FALSE # FALSE FALSE %xor% TRUE # TRUE #### All-or-nothing #### TRUE %aon% TRUE # TRUE FALSE %aon% FALSE # TRUE FALSE %aon% TRUE # FALSE # Apply a regular expression/substitution to x: x <- c("foo", "bar", "dOe", "rei", "mei", "obo") # where x has an O x[x %rlike% "O"] # [1] "foo" "dOe" "obo" # find x where middle letter is "O" x[x %rlike% "[a-z]O[a-z]"] # will print [1] "foo" "dOe" # Apply a regular expression/substitution to x: x <- c("foo", "bar", "dOe", "rei", "mei", "obo") # find x where middle letter is upper-case "O" x[x %perl% "[a-z]O[a-z]"] # will print [1] "dOe"
#### Not in #### "z" %ni% c("a", "b", "c") # TRUE #### Exclusive or #### TRUE %xor% TRUE # FALSE FALSE %xor% FALSE # FALSE FALSE %xor% TRUE # TRUE #### All-or-nothing #### TRUE %aon% TRUE # TRUE FALSE %aon% FALSE # TRUE FALSE %aon% TRUE # FALSE # Apply a regular expression/substitution to x: x <- c("foo", "bar", "dOe", "rei", "mei", "obo") # where x has an O x[x %rlike% "O"] # [1] "foo" "dOe" "obo" # find x where middle letter is "O" x[x %rlike% "[a-z]O[a-z]"] # will print [1] "foo" "dOe" # Apply a regular expression/substitution to x: x <- c("foo", "bar", "dOe", "rei", "mei", "obo") # find x where middle letter is upper-case "O" x[x %perl% "[a-z]O[a-z]"] # will print [1] "dOe"
Determine the current operating system as well as provide flags to indicate whether the operating system is a Mac/Windows/Linux.
get_os() get_R_version() get_R_version_age(units = c("years", "months", "weeks", "days"), rounding = 2) get_latest_CRAN_version() get_system_python() is.os_mac() is.os_win() is.os_lnx() is.os_unx() is.os_x64() is.os_arm() is.R_x64() is.R_revo() is.RStudio() is.http_available()
get_os() get_R_version() get_R_version_age(units = c("years", "months", "weeks", "days"), rounding = 2) get_latest_CRAN_version() get_system_python() is.os_mac() is.os_win() is.os_lnx() is.os_unx() is.os_x64() is.os_arm() is.R_x64() is.R_revo() is.RStudio() is.http_available()
units |
character - how do you want to display the age? e.g. years or months? |
rounding |
integer - how many decimal points do you want to see. e.g. 0.25 years |
Ben Wiseman, [email protected]
Steven Nydick, [email protected]
# determine operating system get_os() # do we have a particular operating system is.os_mac() is.os_win() is.os_lnx() is.os_unx()
# determine operating system get_os() # do we have a particular operating system is.os_mac() is.os_win() is.os_lnx() is.os_unx()
The available functions are:
paste_()
is the same as paste0
but uses an underscore to separate
cat0()
is analogous to paste0
but for cat
catN()
is the same as cat0
but automatically inserts a new line after the cat
paste_series()
paste a series of things with a conjunction
paste_oxford()
shortcut for paste_series
as oxford comma
paste_(..., collapse = NULL) cat0(..., file = "", fill = FALSE, labels = NULL, append = FALSE) catN(..., file = "", fill = FALSE, labels = NULL, append = FALSE) paste_series( ..., sep = c(",", ";"), conjunction = c("and", "or", "&"), use_oxford_comma = TRUE ) paste_oxford(...)
paste_(..., collapse = NULL) cat0(..., file = "", fill = FALSE, labels = NULL, append = FALSE) catN(..., file = "", fill = FALSE, labels = NULL, append = FALSE) paste_series( ..., sep = c(",", ";"), conjunction = c("and", "or", "&"), use_oxford_comma = TRUE ) paste_oxford(...)
... |
one or more R objects, to be converted to character vectors. |
collapse |
an optional character string to separate the results. Not
|
file |
character - A connection, or a character string naming the file to print to. If "" (the default), cat prints to the standard output connection, the console unless redirected by sink. |
fill |
a logical or (positive) numeric controlling how the output is broken into successive lines. see |
labels |
character vector of labels for the lines printed. Ignored if fill is FALSE. |
append |
logical. Only used if the argument |
sep |
a character vector of strings to append after each element |
conjunction |
indicates the ending conjunction. e.g. setting to "and" would make c("a", "b", "c") paste into "a, b, and c" |
use_oxford_comma |
logical - do you want to use an oxford comma at the end? |
Steven Nydick, [email protected]
paste_series("a") paste_series("a", "b") paste_series("a", "b", "c") # works if putting entries into c function paste_series(c("a", "b", "c"), "d") # can use oxford comma or not paste_series("a", "b", "c", use_oxford_comma = TRUE) paste_series("a", "b", "c", use_oxford_comma = FALSE) # makes no difference if fewer than 3 items paste_series("a", "b", use_oxford_comma = TRUE)
paste_series("a") paste_series("a", "b") paste_series("a", "b", "c") # works if putting entries into c function paste_series(c("a", "b", "c"), "d") # can use oxford comma or not paste_series("a", "b", "c", use_oxford_comma = TRUE) paste_series("a", "b", "c", use_oxford_comma = FALSE) # makes no difference if fewer than 3 items paste_series("a", "b", use_oxford_comma = TRUE)
like read.csv, but for tsv and default header = TRUE
like read.csv, but for pipe-delineated and defaults to header = TRUE
read.tsv(file, ...) read.psv(file, ...)
read.tsv(file, ...) read.psv(file, ...)
file |
path of file you want to load |
... |
other args used by read.table |
Perform string concatenation and arithmetic is a similar way to other languages. String division is not present in languages like Python, although arguably it is more useful than string multiplication and can be used with regulr expressions.
x %+% y x %-% y x %s*% y x %s/% y
x %+% y x %-% y x %s*% y x %s/% y
x |
a string |
y |
a string |
Ben Wiseman, [email protected]
("ab" %+% "c") == "abc" # TRUE ("abc" %-% "b") == "ac" # TRUE ("ac" %s*% 2) == "acac" # TRUE ("acac" %s/% "c") == 2 # TRUE # String division with a regular expression: 'an apple a day keeps the malignant spirit of Steve Jobs at bay' %s/% 'Steve Jobs|apple'
("ab" %+% "c") == "abc" # TRUE ("abc" %-% "b") == "ac" # TRUE ("ac" %s*% 2) == "acac" # TRUE ("acac" %s/% "c") == 2 # TRUE # String division with a regular expression: 'an apple a day keeps the malignant spirit of Steve Jobs at bay' %s/% 'Steve Jobs|apple'
Misc/useful type checks to prevent duplicated code
is.scalar(x) is.scalar_or_null(x) is.numeric_or_null(x) is.character_or_null(x) is.logical_or_null(x) is.df_or_null(x) is.list_or_null(x) is.atomic_nan(x) is.irregular_list(x) is.bad_for_calcs(x, na.rm = FALSE) any_bad_for_calcs(x, ..., na.rm = FALSE) all_good_for_calcs(x, ..., na.rm = FALSE) is.bad_for_indexing(x) is.good_for_indexing(x) is.bad_and_equal(x, y) is.bad_for_calcs(x, na.rm = FALSE) is.good_for_calcs(x, na.rm = FALSE) is.null_or_na(x)
is.scalar(x) is.scalar_or_null(x) is.numeric_or_null(x) is.character_or_null(x) is.logical_or_null(x) is.df_or_null(x) is.list_or_null(x) is.atomic_nan(x) is.irregular_list(x) is.bad_for_calcs(x, na.rm = FALSE) any_bad_for_calcs(x, ..., na.rm = FALSE) all_good_for_calcs(x, ..., na.rm = FALSE) is.bad_for_indexing(x) is.good_for_indexing(x) is.bad_and_equal(x, y) is.bad_for_calcs(x, na.rm = FALSE) is.good_for_calcs(x, na.rm = FALSE) is.null_or_na(x)
x |
object to be tested |
na.rm |
If true, NA values aren't considered bad for calculations |
... |
Values to be testes |
y |
object to be tested |
a logical value
Steven Nydick, [email protected]