Skip to content

R

cotrWhileLoop

R While Loop

while (${1:condition}) {
# Your code here
}

cotrEntry

R Entry Point

# R Entry Point
# To run this program, use: `Rscript filename.R`
# Your code here

cotrEqual

R Equal To

==

cotrForLoop

R For Loop

for (${1:i} in ${2:1:10}) {
# Your code here
}

cotrDynamic

R Dynamic Type

# R is a dynamically typed language.

cotrVarTyped

R Create Typed Variable

# Note: R is dynamically typed; explicit type annotations are not used.
${1:my_var} <- $2

cotrTernary

R Ternary Operator

ifelse(${1:condition}, ${2:trueValue}, ${3:falseValue})

cotrOperators

R Mathematical Operators

# R Mathematical Operators
# Addition: +
# Subtraction: -
# Multiplication: *
# Exponentiation: ^
# Division: /
# Modulus (Remainder): %%
# Integer Division: %/%
# Assignment: <- or =
# Addition assignment: +=
# Subtraction assignment: -=
# Multiplication assignment: *=
# Division assignment: /=
# Modulus assignment: %%=
# Exponentiation assignment: ^=

cotrTypesBoolTrue

R Boolean True

TRUE

cotrFunc

R Function

${2:my_function} <- function(${3:parameters}) {
${4:# Your code here}
}

cotrConcat

R Concatenate String

paste(${1:"string1"}, ${2:"string2"}, sep = "")

cotrVarListAlt

R Create List Variable

${1:my_list} <- list(${2:items})

cotrIfElse

R If Else Statement

if (${1:condition}) {
# Your code here
} else {
# Your code here
}

cotrFuncArgs

R Function Args

# In R, functions can have arguments with default values.
${2:my_function} <- function(${3:arg1}, ${4:arg2} = ${5:'defaultVal'}) {
${6:# Your code here}
}

cotrInterpolate

R Interpolate String

paste0("Your string here ", ${1:variable})

cotrVarDate

R Create Date Variable

${1:my_date} <- as.Date('${2:YYYY-MM-DD}')

cotrComment

R Comment

# ${1:Your comment here}

cotrNotEqual

R Not Equal To

!=

cotrTypeCheck

R Type Check

class(${1:variable})

cotrOperatorsBool

R Boolean Operators

# R Boolean Operators
# Logical AND: & or &&
# Logical OR: | or ||
# Logical NOT: !
# Equality: ==
# Inequality: !=
# Greater than: >
# Less than: <
# Greater than or equal to: >=
# Less than or equal to: <=

cotrTypesList

R List Type

list

cotrVarDouble

R Create Double Variable

${1:my_double} <- ${2:0.0}

cotrPrintMulti

R Print Multi

cat(${1:"Line 1",
${2:"Line 2",
${3:"Line 3")

cotrForEachLoop

R For Each Loop

for (item in ${1:iterable}) {
# Your code here
}

cotrIf

R If Statement

if (${1:condition}) {
# Your code here
}

cotrThrow

R Throw Exception

stop(${1:'Your message here'})

cotrInfo

R Info

# Typing: Dynamically typed
# Paradigm: Multi-paradigm: functional, object-oriented, procedural, statistical
# Compilation: Interpreted
# Concurrency: Supports multi-threading with packages like 'parallel'

cotrNow

R Date Now

Sys.Date()

cotrVarString

R Create String Variable

${1:my_string} <- '${2:Your string here}'

cotrTypeCompare

R Type Comparison

# Check if two variables have the same type:
if (identical(class(${1:variable1}), class(${2:variable2}))) {
# Your code here
}

cotrTypeConvert

R Type Conversion

# R Type Conversion:
# Implicit conversions (R performs automatically):
# - Can be unpredictable, especially with loose equality (==).
# Explicit conversions:
# - as.integer(variable) // Converts to integer
# - as.double(variable) // Converts to double
# - as.character(variable) // Converts to string
# - as.logical(variable) // Converts to boolean
# - ... (Various conversion functions in base R and packages)
# Note:
# - Be aware of implicit conversions and use explicit conversions when necessary for clarity and control.

cotrTypesBoolFalse

R Boolean False

FALSE

cotrVarBool

R Create Boolean Variable

${1:my_bool} <- ${2:TRUE}

cotrTypesDate

R Date Type

Date

cotrVarList

R Create Array Variable

${1:my_array} <- c(${2:items})

cotrTypesString

R String Type

character

cotrTypesBool

R Boolean Type

logical

cotrVarStringMulti

R Create Multiline String Variable

${1:my_string} <- paste0(
${2:"Line 1",
${3:"Line 2",
${4:"Line 3")

cotrTryCatch

R Try Catch

tryCatch({
# Your code here
}, error = function(e) {
# Your code here
})

cotrClass

R Class

# R does not have a built-in class type. Use a list or vector instead.

cotrTypes

R Types

$BLOCK_COMMENT_START
R is a dynamically typed language.
Some common types in R include:
- numeric: Numeric data type (includes integers and floating-point numbers)
- character: String
- logical: Boolean
- vector: One-dimensional array
- list: Ordered collection of elements
- data.frame: Two-dimensional data structure
- matrix: Two-dimensional array
- factor: Categorical data
- NULL: Represents the absence of a value
- function: Function object
$BLOCK_COMMENT_END

cotrTypesInt

R Integer Type

# R uses 'numeric' for both integers and floating-point numbers.

cotrTypesMap

R Map Type

# R does not have a built-in Map type. Use lists or data.frames for similar functionality.

cotrNull

R Null Type

NULL

cotrVar

R Create Variable

${1:my_var} <- $2

cotrVarInt

R Create Integer Variable

${1:my_int} <- ${2:0}

cotrCommentMulti

R Multi-Line Comment

${1:comment}

cotrStructure

R Project Structure (High-Level)

# Recommended High-Level R Project Structure:
# - /
# - README.md: Project description and instructions.
# - .Rproj: RStudio project file.
# - R/
# - Contains R script files (.R).
# - Organize by feature or functionality, for example:
# - data_preprocessing.R
# - statistical_models.R
# - visualization.R
# - data/
# - Raw and processed data files.
# - docs/
# - Documentation files, including analysis reports.
# - tests/
# - Test scripts for your R functions.
# - man/
# - Documentation for your functions, if you are building an R package.
# - vignettes/
# - Longer form documentation, such as tutorials or extended use cases.
# Note:
# - This structure can be adapted based on the scale and needs of the project.
# - Use RStudio projects to manage workspace settings and project-specific options.

cotrTypesNum

R Double Type

# R uses 'numeric' for both integers and floating-point numbers.

cotrEnum

R Enum

# R does not have a built-in enum type. Use a list or vector instead.

cotrConst

R Create Constant

${1:MY_CONST} <- $2

cotrPrint

R Print

print(${1:'Your message here'})