die <- c(1, 2, 3, 4, 5, 6) die
## [1] 1 2 3 4 5 6
is.vector(die)
## [1] TRUE
five <- 5 five
## [1] 5
is.vector(five)
## [1] TRUE
length(five)
## [1] 1
length(die)
## [1] 6
int <- 1L int <- c(1L, 5L) sum(int)
## [1] 6
die <- c(1, 2, 3, 4, 5, 6) die
## [1] 1 2 3 4 5 6
typeof(die)
## [1] "double"
int <- c(-1L, 2L, 4L) int
## [1] -1 2 4
typeof(int)
## [1] "integer"
sqrt(2)^2 - 2
## [1] 4.440892e-16
text <- c("Hello", "World") text
## [1] "Hello" "World"
typeof(text)
## [1] "character"
typeof("Hello")
## [1] "character"
3 > 4
## [1] FALSE
logic <- c(TRUE, FALSE, TRUE) logic
## [1] TRUE FALSE TRUE
typeof(logic)
## [1] "logical"
typeof(F)
## [1] "logical"
comp <- c(1 + 1i, 1 + 2i, 1 + 3i) comp
## [1] 1+1i 1+2i 1+3i
typeof(comp)
## [1] "complex"
raw(3)
## [1] 00 00 00
typeof(raw(3))
## [1] "raw"
m <- matrix(die, nrow = 2) m
## [,1] [,2] [,3] ## [1,] 1 3 5 ## [2,] 2 4 6
m <- matrix(die, nrow = 2, byrow = TRUE) m
## [,1] [,2] [,3] ## [1,] 1 2 3 ## [2,] 4 5 6
ar <- array(c(11:14, 21:24, 31:34), dim = c(2, 2, 3)) ar
## , , 1 ## ## [,1] [,2] ## [1,] 11 13 ## [2,] 12 14 ## ## , , 2 ## ## [,1] [,2] ## [1,] 21 23 ## [2,] 22 24 ## ## , , 3 ## ## [,1] [,2] ## [1,] 31 33 ## [2,] 32 34
## , , 1 ## ## [,1] [,2] ## [1,] 11 13 ## [2,] 12 14 ## ## , , 2 ## ## [,1] [,2] ## [1,] 21 23 ## [2,] 22 24 ## ## , , 3 ## ## [,1] [,2] ## [1,] 31 33 ## [2,] 32 34 ## [,1] [,2] ## [1,] "ace" "spades" ## [2,] "king" "spades" ## [3,] "queen" "spades" ## [4,] "jack" "spades" ## [5,] "ten" "spades" hand1 <- c("ace", "king", "queen", "jack", "ten", "spades", "spades", "spades", "spades", "spades") matrix(hand1, nrow = 5)
## [,1] [,2] ## [1,] "ace" "spades" ## [2,] "king" "spades" ## [3,] "queen" "spades" ## [4,] "jack" "spades" ## [5,] "ten" "spades"
matrix(hand1, ncol = 2)
## [,1] [,2] ## [1,] "ace" "spades" ## [2,] "king" "spades" ## [3,] "queen" "spades" ## [4,] "jack" "spades" ## [5,] "ten" "spades"
dim(hand1) <- c(5, 2) hand2 <- c("ace", "spades", "king", "spades", "queen", "spades", "jack", "spades", "ten", "spades") matrix(hand2, nrow = 5, byrow = TRUE)
## [,1] [,2] ## [1,] "ace" "spades" ## [2,] "king" "spades" ## [3,] "queen" "spades" ## [4,] "jack" "spades" ## [5,] "ten" "spades"
matrix(hand2, ncol = 2, byrow = TRUE)
## [,1] [,2] ## [1,] "ace" "spades" ## [2,] "king" "spades" ## [3,] "queen" "spades" ## [4,] "jack" "spades" ## [5,] "ten" "spades"
list1 <- list(100:130, "R", list(TRUE, FALSE)) list1
## [[1]] ## [1] 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 ## [20] 119 120 121 122 123 124 125 126 127 128 129 130 ## ## [[2]] ## [1] "R" ## ## [[3]] ## [[3]][[1]] ## [1] TRUE ## ## [[3]][[2]] ## [1] FALSE
card <- list("ace", "hearts", 1) card
## [[1]] ## [1] "ace" ## ## [[2]] ## [1] "hearts" ## ## [[3]] ## [1] 1
df <- data.frame(face = c("ace", "two", "six"), suit = c("clubs", "clubs", "clubs"), value = c(1, 2, 3)) df
## face suit value ## 1 ace clubs 1 ## 2 two clubs 2 ## 3 six clubs 3
typeof(df)
## [1] "list"
class(df)
## [1] "data.frame"
str(df)
## 'data.frame': 3 obs. of 3 variables: ## $ face : chr "ace" "two" "six" ## $ suit : chr "clubs" "clubs" "clubs" ## $ value: num 1 2 3
df <- data.frame(face = c("ace", "two", "six"), suit = c("clubs", "clubs", "clubs"), value = c(1, 2, 3), stringsAsFactors = FALSE) ## face suit value ## king spades 13 ## queen spades 12 ## jack spades 11 ## ten spades 10 ## nine spades 9 ## eight spades 8 ## seven spades 7 ## six spades 6 ## five spades 5 ## four spades 4 ## three spades 3 ## two spades 2 ## ace spades 1 ## king clubs 13 ## queen clubs 12 ## jack clubs 11 ## ten clubs 10 ## ... and so on. deck <- data.frame( face = c("king", "queen", "jack", "ten", "nine", "eight", "seven", "six", "five", "four", "three", "two", "ace", "king", "queen", "jack", "ten", "nine", "eight", "seven", "six", "five", "four", "three", "two", "ace", "king", "queen", "jack", "ten", "nine", "eight", "seven", "six", "five", "four", "three", "two", "ace", "king", "queen", "jack", "ten", "nine", "eight", "seven", "six", "five", "four", "three", "two", "ace"), suit = c("spades", "spades", "spades", "spades", "spades", "spades", "spades", "spades", "spades", "spades", "spades", "spades", "spades", "clubs", "clubs", "clubs", "clubs", "clubs", "clubs", "clubs", "clubs", "clubs", "clubs", "clubs", "clubs", "clubs", "diamonds", "diamonds", "diamonds", "diamonds", "diamonds", "diamonds", "diamonds", "diamonds", "diamonds", "diamonds", "diamonds", "diamonds", "diamonds", "hearts", "hearts", "hearts", "hearts", "hearts", "hearts", "hearts", "hearts", "hearts", "hearts", "hearts", "hearts", "hearts"), value = c(13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1) )
You can also embed plots, for example:
plot(cars)
