x <- 1 # put a space on each side of the assignment sign
x # type variable name to print its value(s)
[1] 1
[1] "InforBio"
Error: object 'InforBio' not found
IOC-R Week 2
<-
for assignment.x <- 1 # put a space on each side of the assignment sign
x # type variable name to print its value(s)
[1] 1
[1] "InforBio"
Error: object 'InforBio' not found
Check the “Environment” pane or type ls()
in the console, are variables you just created there?
!
, #
, ) and spacesHow to know if a variable name was already used?
help(reserved)
to check reserved words in R.Examples: 1
, 2.5
, "A"
, "InforBio"
, "I love R"
, TRUE
, FALSE
, …
How R understands and stores information?
Main data types:
2.5
1
"A"
, "InforBio"
, "I love R"
TRUE
, FALSE
We’ll see factor next week.
R stores text (strings) as character. Use quotation marks to indicate a value is character.
[1] "I love R"
[1] TRUE
Only two possible values for logical data: TRUE
or FALSE
.
Can be written as T
or F
, but never in other formats (e.g.: True
, true
)
Convert to other types:
When we store multiple values, we need a structure.
How to put gene1
, gene2
and gene3
together?
R provides 4 data structures to store multiple values:
1 dimension | 2 dimensions (row/column) | |
---|---|---|
Same data type | vector | matrix |
Different data types | list | data frame |
The simplest data structure in R, for one dimension data of the same type.
Use the function c()
to create a vector and use ,
to separate elements.
[1] 10 12 9
[1] 10 12 9
[1] 10 12 9
[1] 10 12 9 18
[1] "gene1" "gene2" "gene3"
[1] TRUE FALSE FALSE TRUE TRUE
When you combine different data types …
[1] 10 1
[1] "10" "gene1"
[1] "TRUE" "gene1"
[1] "10" "gene1" "TRUE"
R follows a hierarchy of data types for coercion:
logical (least inclusive) → numeric → character (most inclusive)
[
idx]
to access element(s).Notes: The index starts from 1.
[
name]
to access element(s) if the vector is named.[1] 1 2 5 4
[1] TRUE TRUE FALSE FALSE
[1] 1 2
A matrix is a two dimensional data structure with rows and columns, it contains data of the same data type.
matrix()
function to create a matrix.[
row_idx,
column_idx]
to access element(s).[
row_name,
column_name]
to access element(s) if names exist.[1] TRUE
int [1:3, 1:4] 1 2 3 4 5 6 7 8 9 10 ...
- attr(*, "dimnames")=List of 2
..$ : chr [1:3] "r1" "r2" "r3"
..$ : chr [1:4] "c1" "c2" "c3" "c4"
When assigning new values, you must provide either: