v <- c(1, 2, 3)
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 |
a <- 3.14
b <- 5L
c <- "Hello"
d <- TRUE
e <- 1 + 2i
class(a)
is.character(c)
is.logical(d)c() which stands for concatenate.[], where [1] is the first element.v <- c(1, 2, 3)
names(v) <- c("One", "Two", "Three")
# check for dimension
dim(v)NULL
m <- matrix(1:6, nrow = 2, ncol = 3)
m [,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
myrows <- c("A", "B", "C", "D")
mycol <- c("col1", "col2", "col3")
x <- matrix(1:12, nrow = 4, ncol = 3, dimnames = list(myrows, mycol))
x col1 col2 col3
A 1 5 9
B 2 6 10
C 3 7 11
D 4 8 12
x[1:2, 2:3] #accessing elements in the matrix col2 col3
A 5 9
B 6 10
[ ], [[ ]], and $.my_list <- list(name="Alice", age=25, scores=c(90, 95, 88))
# Accessing list elements
my_list[1] # Returns a list$name
[1] "Alice"
class(my_list[1]) # "list"[1] "list"
my_list[[1]] # Returns the element in its original type[1] "Alice"
class(my_list[[1]]) # "character"[1] "character"
my_list$name # Returns "Alice"[1] "Alice"
df <- data.frame(
name = c("John", "Sara"),
age = c(25, 30),
score = c(80, 95)
)
df name age score
1 John 25 80
2 Sara 30 95
g <- factor(c("male", "female", "female", "male"))
g[1] male female female male
Levels: female male