R
Beginner
1 min read
Getting Started with R
Example
# R uses <- for assignment (= also works but <- is idiomatic)
x <- 42
name <- "Alice"
is_valid <- TRUE
# Print values to the console
print(x)
cat("Hello,", name, "\n")
# Basic arithmetic
a <- 10
b <- 3
cat("Sum: ", a + b, "\n")
cat("Diff: ", a - b, "\n")
cat("Product: ", a * b, "\n")
cat("Quotient:", a / b, "\n")
cat("Integer div:", a %/% b, "\n")
cat("Modulo: ", a %% b, "\n")
cat("Power: ", a ^ b, "\n")
# Check data types
class(x) # "numeric"
class(name) # "character"
class(is_valid) # "logical"
typeof(x) # "double"
# Accessing built-in help
# ?mean # opens help page for mean()
# help("lm") # opens help page for lm()
# Simple sequence and basic stats
nums <- 1:20
cat("Length:", length(nums), "\n")
cat("Mean: ", mean(nums), "\n")
cat("Sum: ", sum(nums), "\n")