R Basic Syntax

Essential R syntax and programming basics

Variables

x <- 5 # assignment with <-
y = 10 # assignment with =
name <- "John" # string variable
is_active <- TRUE # logical variable

Data Types

numeric # numbers (3.14, 5)
integer # integers (5L)
character # strings ("text")
logical # TRUE or FALSE
complex # complex numbers (1+2i)

Check Type

class(x) # get object class
typeof(x) # get internal type
is.numeric(x) # check if numeric
is.character(x) # check if character

Print Output

print("Hello World") # print with quotes
cat("Hello", "World") # concatenate and print
paste("Hello", "World") # paste strings

Comments

# single line comment