<- c(1, 2, 3)
v names(v) <- c("One", "Two", "Three")
# check for dimension
dim(v)
NULL
By the end of this class, you will be able to:
class()
, typeof()
, and str()
.Console
Source pane
Environment/History
Plots/Files/Packages
Data Type | Description | Example |
---|---|---|
numeric |
Real numbers | 3.14 |
integer |
Whole numbers | 5L |
character |
Text strings | "Hello" |
logical |
Boolean (TRUE/FALSE) | TRUE |
complex |
Complex numbers | 1 + 2i |
<- 3.14
a <- 5L
b <- "Hello"
c <- TRUE
d <- 1 + 2i
e
class(a)
is.character(c)
is.logical(d)
c()
which stands for concatenate.[]
, where [1]
is the first element.<- c(1, 2, 3)
v names(v) <- c("One", "Two", "Three")
# check for dimension
dim(v)
NULL
<- matrix(1:6, nrow = 2, ncol = 3)
m m
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
<- c("A", "B", "C", "D")
myrows <- c("col1", "col2", "col3")
mycol
<- matrix(1:12, nrow = 4, ncol = 3, dimnames = list(myrows, mycol))
x
x
col1 col2 col3
A 1 5 9
B 2 6 10
C 3 7 11
D 4 8 12
1:2, 2:3] #accessing elements in the matrix x[
col2 col3
A 5 9
B 6 10
[ ]
, [[ ]]
, and $
.<- list(name="Alice", age=25, scores=c(90, 95, 88))
my_list
# Accessing list elements
1] # Returns a list my_list[
$name
[1] "Alice"
class(my_list[1]) # "list"
[1] "list"
1]] # Returns the element in its original type my_list[[
[1] "Alice"
class(my_list[[1]]) # "character"
[1] "character"
$name # Returns "Alice" my_list
[1] "Alice"
<- data.frame(
df name = c("John", "Sara"),
age = c(25, 30),
score = c(80, 95)
)
df
name age score
1 John 25 80
2 Sara 30 95
<- factor(c("male", "female", "female", "male"))
g g
[1] male female female male
Levels: female male