1 + 1[1] 2
3 * (4 + 5)[1] 27
1 + 1[1] 2
3 * (4 + 5)[1] 27
x <- 10
y <- 3.5
x + y[1] 13.5
age, Age, and AGE would be considered different variables.R supports various basic operations, including: * Arithmetic Operations: Addition (+), subtraction (-), multiplication (*), division (/), and exponentiation (^). Example:
a <- 10
b <- 5
sum <- a + b
diff <- a - b
prod <- a * b
quot <- a / b
exp <- a ^ b
sum; diff; prod; quot; exp[1] 15
[1] 5
[1] 50
[1] 2
[1] 1e+05
==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=). Example:x <- 10
y <- 5
eq <- x == y
neq <- x != y
gt <- x > y
lt <- x < y
gte <- x >= y
lte <- x <= y
eq; neq; gt; lt; gte; lte[1] FALSE
[1] TRUE
[1] TRUE
[1] FALSE
[1] TRUE
[1] FALSE
&), OR (|), and NOT (!). Example:p <- TRUE
q <- FALSE
and <- p & q
or <- p | q
not <- !p
and; or; not[1] FALSE
[1] TRUE
[1] FALSE
R provides several ways to get help and documentation for functions and packages: * ?function_name: Displays the documentation for a specific function. Example:
?meanhelp(function_name): Another way to access the documentation for a function. Example:help(mean)help.search("keyword"): Searches for help topics related to a specific keyword. Example:help.search("regression")example(function_name): Shows examples of how to use a specific function. Example:example(mean)
mean> x <- c(0:10, 50)
mean> xm <- mean(x)
mean> c(xm, mean(x, trim = 0.10))
[1] 8.75 5.50
vignette("package_name"): Opens the vignette (detailed documentation) for a specific package. Example:vignette("dplyr")starting httpd help server ... done
??keyword: Searches for help topics related to a specific keyword (similar to help .search). Example:??regressionR has a vast ecosystem of packages that extend its functionality. To use a package, you need to install it first and then load it into your R session. * Installing a Package: Use the install.packages("package_name") function to install a package from CRAN. Example:
install.packages("ggplot2")library(package_name) function to load an installed package into your R session. Example:library(ggplot2)
# Now you can use functions from the ggplot2 packageYou can save your R workspace (all objects in memory) to a file and load it later * Saving Workspace: Use the save.image("file_name.RData") function to save the entire workspace to a file. Example:
save.image("my_workspace.RData")load("file_name.RData") function to load a saved workspace from a file. Example:load("my_workspace.RData")getwd()[1] "/home/runner/work/r4sas/r4sas"
# setwd("/path/you/want") # avoid in reproducible code; prefer here::here() for projectsnums <- c(1, 2, 3, 4)
chars <- c("a", "b", "c")
logical <- c(TRUE, FALSE, TRUE)
typeof(nums); typeof(chars); typeof(logical)[1] "double"
[1] "character"
[1] "logical"
z that stores (5^2 + 7)/3.?seq and create a sequence from 0 to 1 by 0.1.typeof() for a few objects you create.
7 Comments in R
Comments in R are created using the
#symbol. Anything following the#on the same line is considered a comment and is ignored by R during execution. Example: