| Title: | Modern Base-R Functions |
|---|---|
| Description: | Includes modern base-R functions. Functions beginning with p_ are wrapper functions for existing base-R functions, supporting native piping. Other functions are wrapper functions for core base-R features, including bracket notation and dollar-sign notation. base_match() and base_when() mimic case_match() and case_when() from 'dplyr' but return a factor by default with levels ordered according to user input. et() mimics count() from 'dplyr'. |
| Authors: | Yea-Hung Chen [aut, cre] |
| Maintainer: | Yea-Hung Chen <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.4 |
| Built: | 2026-05-31 06:13:48 UTC |
| Source: | https://github.com/yea-hung/baseverse |
A wrapper function for !.
bang(x)bang(x)
x |
a logical object |
a logical object
# load the data data(nhanes) # check whether values of smq020 are not missing nhanes$smq020 |> is.na() |> bang() # check whether values of smq020 are not missing nhanes |> dollar(smq020) |> is.na() |> bang()# load the data data(nhanes) # check whether values of smq020 are not missing nhanes$smq020 |> is.na() |> bang() # check whether values of smq020 are not missing nhanes |> dollar(smq020) |> is.na() |> bang()
A base-R approximation of case_match() from 'dplyr'. Unlike case_match(), base_when() returns a factor. The levels will be ordered according to the order included in ... (see below).
base_match(original_variable, ..., as_factor = TRUE, string_for_na = "")base_match(original_variable, ..., as_factor = TRUE, string_for_na = "")
original_variable |
the original variable |
... |
the codebook, specified as a named vector, with each element in |
as_factor |
logical, controlling whether the function should return a factor |
string_for_na |
string value that will be converted to NA |
a factor
# load data data(nhanes) # define country nhanes<-nhanes |> transform( country=base_match(dmdborn4,'USA'=1,'Other'=2) )# load data data(nhanes) # define country nhanes<-nhanes |> transform( country=base_match(dmdborn4,'USA'=1,'Other'=2) )
A base-R approximation of case_when() from 'dplyr'. Unlike case_when(), base_when() returns a factor. The levels will be ordered according to the order included in ... (see below).
base_when(..., as_factor = TRUE, string_for_na = "")base_when(..., as_factor = TRUE, string_for_na = "")
... |
conditions for defining the replacement values, specified as a named list, with each element in |
as_factor |
logical, controlling whether the function should return a factor |
string_for_na |
string value that will be converted to NA |
a factor
# load data data(nhanes) # define cholesterol nhanes<-nhanes |> transform( cholesterol=base_when( 'Desirable' = (lbxtc<200), 'Borderline high' = (lbxtc>=200)&(lbxtc<240), 'High' = (lbxtc>=240) ) )# load data data(nhanes) # define cholesterol nhanes<-nhanes |> transform( cholesterol=base_when( 'Desirable' = (lbxtc<200), 'Borderline high' = (lbxtc>=200)&(lbxtc<240), 'High' = (lbxtc>=240) ) )
A wrapper function for bracket notation. The function only allows one index; for two-dimensional objects, the index is used on the columns.
bracket(data_object, index)bracket(data_object, index)
data_object |
a data object |
index |
a character, logical, or numeric vector |
a subset of the data
# load package data(nhanes) # look at the first few values of dmdborn4 nhanes |> bracket('dmdborn4') |> head()# load package data(nhanes) # look at the first few values of dmdborn4 nhanes |> bracket('dmdborn4') |> head()
A wrapper function for dollar-sign notation.
dollar(data_object, variable_name)dollar(data_object, variable_name)
data_object |
the data object |
variable_name |
the variable name or element name |
a portion of the data object
# load data data(nhanes) # summarize lbxtc nhanes |> dollar(lbxtc) |> summary()# load data data(nhanes) # summarize lbxtc nhanes |> dollar(lbxtc) |> summary()
Creates a table for a variable, mimicking count() from 'dplyr'. ET stands for exploratory table or easy table.
et(data_frame, variable_name)et(data_frame, variable_name)
data_frame |
the data.frame |
variable_name |
the variable name |
a data frame
# load data data(nhanes) # define country nhanes<-nhanes |> transform( country=base_match(dmdborn4,'USA'=1,'Other'=2) ) # examine variable, with native piping nhanes |> et(country) # examine variable, with dollar-sign notation et(nhanes$country)# load data data(nhanes) # define country nhanes<-nhanes |> transform( country=base_match(dmdborn4,'USA'=1,'Other'=2) ) # examine variable, with native piping nhanes |> et(country) # examine variable, with dollar-sign notation et(nhanes$country)
A subset of data from the National Health and Nutrition Examination Survey
nhanesnhanes
nhanesA data frame with 8,153 rows and 3 columns:
a unique identifier
gender
age
military status
country of birth
total cholesterol
systolic blood pressure
systolic blood pressure
diastolic blood pressure
diastolic blood pressure
asthma
smoking status
https://wwwn.cdc.gov/nchs/nhanes/continuousnhanes/default.aspx?Cycle=2021-2023
A wrapper function for !. This is the same thing as bang().
not(x)not(x)
x |
a logical object |
a logical object
# load the data data(nhanes) # check whether values of smq020 are not missing nhanes$smq020 |> is.na() |> not() # check whether values of smq020 are not missing nhanes |> dollar(smq020) |> is.na() |> not()# load the data data(nhanes) # check whether values of smq020 are not missing nhanes$smq020 |> is.na() |> not() # check whether values of smq020 are not missing nhanes |> dollar(smq020) |> is.na() |> not()
A wrapper function for cor(), with support for piping.
p_cor(data, x, y, ...)p_cor(data, x, y, ...)
data |
the data |
x |
one of the two variables |
y |
one of the two variables |
... |
additional arguments passed to |
a numeric object
# load the data data(nhanes) # get correlation coefficient for systolic and diastolic nhanes |> p_cor(bpxosy1,bpxodi1,use='complete.obs')# load the data data(nhanes) # get correlation coefficient for systolic and diastolic nhanes |> p_cor(bpxosy1,bpxodi1,use='complete.obs')
A wrapper function for glm(), with the data argument listed first to support piping.
p_glm(data, formula, ...)p_glm(data, formula, ...)
data |
the data |
formula |
the formula |
... |
additional arguments passed to |
a glm object
# load the data data(nhanes) # define asthma nhanes<-nhanes |> transform( asthma=base_match(mcq010,'No'=2,'History of asthma'=1) ) # define smoking nhanes<-nhanes |> transform( smoking=base_match(smq020,'No'=2,'History of smoking'=1) ) # fit a model my_model<-nhanes |> p_glm(asthma~smoking,family=binomial(link='logit')) # obtain model details my_model |> summary() # obtain confidence interval my_model |> confint() |> exp()# load the data data(nhanes) # define asthma nhanes<-nhanes |> transform( asthma=base_match(mcq010,'No'=2,'History of asthma'=1) ) # define smoking nhanes<-nhanes |> transform( smoking=base_match(smq020,'No'=2,'History of smoking'=1) ) # fit a model my_model<-nhanes |> p_glm(asthma~smoking,family=binomial(link='logit')) # obtain model details my_model |> summary() # obtain confidence interval my_model |> confint() |> exp()
A wrapper function for lm(), with the data argument listed first to support native piping.
p_lm(data, formula, ...)p_lm(data, formula, ...)
data |
the data |
formula |
the formula |
... |
additional arguments passed to |
an lm object
# load the data data(nhanes) # define country nhanes<-nhanes |> transform( country=base_match(dmdborn4,'USA'=1,'Other'=2) ) # fit a model my_model<-nhanes |> p_lm(bpxosy1~country) # obtain model details my_model |> summary() # obtain confidence interval my_model |> confint()# load the data data(nhanes) # define country nhanes<-nhanes |> transform( country=base_match(dmdborn4,'USA'=1,'Other'=2) ) # fit a model my_model<-nhanes |> p_lm(bpxosy1~country) # obtain model details my_model |> summary() # obtain confidence interval my_model |> confint()
A wrapper function for t.test(), with the data argument listed first to support piping.
p_t.test(data, x = NULL, y = NULL, formula = NULL, ...)p_t.test(data, x = NULL, y = NULL, formula = NULL, ...)
data |
the data |
x |
one of two variables |
y |
one of two variables |
formula |
a formula |
... |
additional arguments passed to |
an htest object
# load the data data(nhanes) # define smoking nhanes<-nhanes |> transform( smoking=base_match(smq020,'No'=2,'History of smoking'=1) ) # conduct a one-sample t-test nhanes |> p_t.test(bpxosy1) # conduct a two-sample t-test, using formula notation nhanes |> p_t.test(bpxosy1~smoking) # conduct a two-sample t-test, using formula notation nhanes |> p_t.test(formula=bpxosy1~smoking) # conduct a paired t-test, using x and y nhanes |> p_t.test(bpxosy1,bpxosy2,paired=TRUE)# load the data data(nhanes) # define smoking nhanes<-nhanes |> transform( smoking=base_match(smq020,'No'=2,'History of smoking'=1) ) # conduct a one-sample t-test nhanes |> p_t.test(bpxosy1) # conduct a two-sample t-test, using formula notation nhanes |> p_t.test(bpxosy1~smoking) # conduct a two-sample t-test, using formula notation nhanes |> p_t.test(formula=bpxosy1~smoking) # conduct a paired t-test, using x and y nhanes |> p_t.test(bpxosy1,bpxosy2,paired=TRUE)
A wrapper function for table(), with support for piping.
p_table(data, x, y = NULL, ...)p_table(data, x, y = NULL, ...)
data |
the data |
x |
one variable |
y |
another variable |
... |
additional arguments passed to |
a table object
# load the data data(nhanes) # define smoking nhanes<-nhanes |> transform( smoking=base_match(smq020,'No'=2,'History of smoking'=1) ) # define asthma nhanes<-nhanes |> transform( asthma=base_match(mcq010,'Asthma'=1,'No'=2) ) # table smoking nhanes |> p_table(smoking) # table smoking and asthma nhanes |> p_table(smoking,asthma)# load the data data(nhanes) # define smoking nhanes<-nhanes |> transform( smoking=base_match(smq020,'No'=2,'History of smoking'=1) ) # define asthma nhanes<-nhanes |> transform( asthma=base_match(mcq010,'Asthma'=1,'No'=2) ) # table smoking nhanes |> p_table(smoking) # table smoking and asthma nhanes |> p_table(smoking,asthma)
A wrapper function for wilcox.test(), with the data argument listed first to support piping.
p_wilcox.test(data, x = NULL, y = NULL, formula = NULL, ...)p_wilcox.test(data, x = NULL, y = NULL, formula = NULL, ...)
data |
the data |
x |
one of two variables |
y |
one of two variables |
formula |
a formula |
... |
additional arguments passed to |
an htest object
# load the data data(nhanes) # define smoking nhanes<-nhanes |> transform( smoking=base_match(smq020,'No'=2,'History of smoking'=1) ) # conduct a one-sample wilcoxon test nhanes |> p_wilcox.test(bpxosy1) # conduct a two-sample wilcoxon test, using formula notation nhanes |> p_wilcox.test(bpxosy1~smoking) # conduct a two-sample wilcoxon test, using formula notation nhanes |> p_wilcox.test(formula=bpxosy1~smoking) # conduct a paired wilcoxon test, using x and y nhanes |> p_wilcox.test(bpxosy1,bpxosy2,paired=TRUE)# load the data data(nhanes) # define smoking nhanes<-nhanes |> transform( smoking=base_match(smq020,'No'=2,'History of smoking'=1) ) # conduct a one-sample wilcoxon test nhanes |> p_wilcox.test(bpxosy1) # conduct a two-sample wilcoxon test, using formula notation nhanes |> p_wilcox.test(bpxosy1~smoking) # conduct a two-sample wilcoxon test, using formula notation nhanes |> p_wilcox.test(formula=bpxosy1~smoking) # conduct a paired wilcoxon test, using x and y nhanes |> p_wilcox.test(bpxosy1,bpxosy2,paired=TRUE)