Tuesday, 17 September 2024
>+.R or .r.TRUE/FALSE),26L
## [1] 26
2.71828
## [1] 2.71828
0xef
## [1] 239
"Hello"
## [1] "Hello"
NULL
## NULL
Some other special constants that one may come across in mathematical computations
1/0 0/0
## [1] Inf ## [1] NaN
;).1 + 1
## [1] 2
6 / 4
## [1] 1.5
1 + 1; 6 / 4 # identical
## [1] 2
## [1] 1.5
1 + "a"
## Error in 1 + "a": non-numeric argument to binary operator
\) for nested quotes and special characters."He said: "I have a dream""
## Error: <text>:1:12: unexpected symbol ## 1: "He said: "I ## ^
"He said: \"I have a dream!\""
## [1] "He said: \"I have a dream!\""
"The right word is 'cute'."
## [1] "The right word is 'cute'."
<- is the assignment operator.= works as well## Create a variable life <- 42L ## Evaluate the variable by printing out its value life
## [1] 42
Rules:
.) or underscore (_)_+, -, ?, etcmyobject <- "a" my.object <- TRUE .myObject <- 1e-12 _myObject <- 6.3328 # illegal 4myobject <- 6798L # illegal `4myobject` <- 6798L # backquotes make this permissible
sum is different from SumQuiz
How can you tell between a symbol and a character string?base package is the most important packages that is ALWAYS available.utils, stats, methods, graphics, grDevices, datasets+, -, *, /^, %%, %/% (exponentiation, modulo, integer division)&, |, !==, !=<, >, <=, >=Arithmetic
4^3
## [1] 64
7 / 5
## [1] 1.4
7 %% 5
## [1] 2
7 %/% 5
## [1] 1
Logical
TRUE & FALSE
## [1] FALSE
TRUE | FALSE
## [1] TRUE
!FALSE
## [1] TRUE
!TRUE
## [1] FALSE
Comparison
x <- 7L x == 7
## [1] TRUE
x != 7
## [1] FALSE
x < 8
## [1] TRUE
x > 7
## [1] FALSE
x >= 7
## [1] TRUE
x <= 3
## [1] FALSE
To use a function in R:
()func(arg1, arg2) # 1. signature
func(arg1 = "input1", arg2 = 4.333) # 2. function call
func("input1", 4.333) # 3. Same as 2
func(arg2 = 4.333, arg1 = "input1") # 4. Same as 2 & 3
func(4.333, "input1") # 5. Likely to fail
#) sign.# This is a comment - the whole line will be ignored "This is a valid string; it will be evaluated" # but this part won't
## [1] "This is a valid string; it will be evaluated"
getwd() # Displays the current working directory
setwd("path/to/dir") # Sets working directory to a new location
list.files() # Lists contents of directory, defaults to getwd()
ls()
## character(0)
x <- 1e7 ls()
## [1] "x"
rm(x) # appropriate arguments to remove multiple objects ls()
## character(0)
install.packages())library() and :: operator)base package first, then other default packages.search()
## [1] ".GlobalEnv" "package:stats" "package:graphics" ## [4] "package:grDevices" "package:utils" "package:datasets" ## [7] "package:methods" "Autoloads" "package:base"
library(MASS) search()
## [1] ".GlobalEnv" "package:MASS" "package:stats" ## [4] "package:graphics" "package:grDevices" "package:utils" ## [7] "package:datasets" "package:methods" "Autoloads" ## [10] "package:base"
? operator.R Manuals
help.start() # Opens R Manuals (best for reference purpose)
Function help
?sum
help("sum") # Help for the function 'sum'
Package documentation
help(package = "MASS") # Help on the MASS package library(help = "MASS") # Summary info on the MASS package