Hi all, this section is about introduction to R codes.

Copy the codes in the grey region onto your R console to run them.

1. Expressions

"Hello World!"
## [1] "Hello World!"
1+1
## [1] 2
print("I'm learning R")
## [1] "I'm learning R"
3^2
## [1] 9

2. Testing logical values (returns “TRUE” or “FALSE”)

8>6
## [1] TRUE
1+3 == 3
## [1] FALSE
2 <= 3
## [1] TRUE

3. R Scripts - download the “start.R” sample here

# loads the script
source("start.R") 

4. Variables

# assigns 8 to x
x <- 8 
# print value of x
x 
## [1] 8
# use x like a variable in expressions
x/2 
## [1] 4
# "=" sign can be used to assign value to variables too
y = "I'm learning R!" 
y
## [1] "I'm learning R!"

5. Functions

# sum
sum(1,2,3)
## [1] 6
# square root
sqrt(25)
## [1] 5
# repeat
rep("hi", 20)
##  [1] "hi" "hi" "hi" "hi" "hi" "hi" "hi" "hi" "hi" "hi" "hi" "hi" "hi" "hi"
## [15] "hi" "hi" "hi" "hi" "hi" "hi"

6. Examples on getting help

# keying "?" infront of functions
?sum
?sqrt
?rep

# keying "??" infront of unknown functions or packages
??qplot
??ggplot2

Return to contents page