NOT functions, genRandImg, vector_shuffle, data_pop, and init function amongst many great new functions in the quickcode R package
Obinna (OBI) Obianom
2023-12-10
Source:vignettes/quickcode_r_introduction.Rmd
quickcode_r_introduction.Rmd
A compilation of NOT functions + some useful functions to simply R programming, keep the console neat, and save memory usage
Install
devtools::install_github("oobianom/quickcode")
install.packages("quickcode") # when published
List of many cool functions to use
%nin% Not in vector or array
add.header Snippet function to add header to a current opened file
add.snippet.clear Snippet R function to clear console and set directory
add_key Add index keys to a vector or data frame or list or matrix
ai.duplicate Prompt guided duplication if files
archivedPkg Listing of all CRAN archived R packages
as.boolean Convert boolean values between formats
clean Clear environment, clear console, set work directory and load files
compHist Compare histograms of two distributions
data_pop Remove last n rows or column or specified elements from a data frame like array_pop in PHP
data_pop_filter Remove elements from a data matching filter
data_push Add data to another data like array_push in PHP
data_shuffle Shuffle a data frame just like shuffle in PHP
duplicate Duplicate a file with global text replace
genRandImg Download random images from the web
geo.cv Calculate geometric coefficient of variation and round
geo.mean Calculate geometric mean and round
geo.sd Calculate geometric standard deviation and round
header.rmd Snippet function to add header to a current Rmd opened file
inc Increment vector by value
init Initialize new variables and objects
insertInText Shiny app function to insert string to current file in RStudio
is.image Is file extension an image
libraryAll Load specific R libraries and clear environment
list_push Add elements to a list like array_push in PHP
minus Decrease vector by value
not.data Not a data
not.duplicated Not duplicated elements
not.empty Not empty
not.environment Not an environment
not.image File name is Not an image
not.integer Not an integer
not.logical Not logical
not.na Not NA
not.null Not NULL
not.numeric Not numeric
not.vector Not a vector
number Generate a random number
plus Increment vector by value
rcolorconst R Color Constant
refresh Clear environment, clear console, set work directory and load files
sample_by_column Re-sample a dataset by column and return number of entry needed
vector_pop Remove last n elements or specified elements from a vector like array_pop in PHP
vector_push Add elements to a vector like array_push in PHP
vector_shuffle Shuffle a vector just like shuffle in PHP
yesNoBool Convert Yes/No to Binary or Logical
Let me show you a few R Examples
#initialize one or more variables
print(g) # Error: object 'g' not found
init(g,h,i,o)
print(g) # g = NULL
print(h) # h = NULL
init(r,y,u,b,value = 5)
print(r) # r = 5
print(b) # b = 5
print(z) # Error: object 'z' not found
#add keys to a vector content for use in downstream processes
ver1 <- c("Test 1","Test 2","Test 3")
add_key(ver1)
for(i in ver1){
message(sprintf("%s is the key for this %s", i$key, i$value))
}
#check if the entry is not integer
not.integer(45) #returns TRUE
not.integer(45.) #returns TRUE
not.integer(45L) #returns FALSE
not.null(45L) #returns TRUE
not.null(h<-NULL) #returns FALSE
#clear R environment, set directory and load data
#note: the code below also automatically loads the quickcode library so that all other functions within package can be used easily
quickcode::refresh()
quickcode::clean()
#or combine with setwd and source and load
quickcode::clean(
setwd = "/wd/",
source = c(
"file.R",
"file2.R"
),
load = c(
"data.RData",
"data2.RData"
)
)
#shorthand for not in vector
p1 <- 4
p2 <- c(1:10)
p1 %nin% p2
#add to a vector in one code
p1 <- c(6,7,8)
p2 <- c(1,2,3)
vector_push(p1,p2)
print(p1)
#add to a data frame in one code
p1 <- data.frame(ID=1:10,ID2=1:10)
p2 <- data.frame(ID=11:20,ID2=21:30)
data_push(p1,p2,"rows")
print(p1)
#remove from a vector in one code
p1 <- c(6,7,8,1,2,3)
vector_pop(p1)
print(p1)
#remove from a data frame in one code
p1 <- data.frame(ID=1:10,ID2=1:10,CD=11:20,BD=21:30)
data_pop(p1) #remove last row
print(p1)
data_pop(p1,5) #remove last 5 rows
print(p1)
#remove columns from a data frame in one code
p1 <- data.frame(ID=1:10,ID2=1:10,ID4=1:10,CD=11:20,BD=21:30)
data_pop(p1,which = "cols") #remove last column
print(p1)
data_pop(p1,2,which = "cols") #remove last 2 columns
print(p1)
data_pop(p1,1,which = "cols") #remove last 1 column and vectorise
print(p1)
#load libraries
quickcode::libraryAll(
dplyr,
r2resize,
ggplot2
)
Check out some examples at quickcode.obi.obianom.com