x <- c(10.4, 5.6, 3.1, 6.4, 21.7)
x
## [1] 10.4 5.6 3.1 6.4 21.7
assign("x", c(10.4, 5.6, 3.1, 6.4, 21.7))
c(10.4, 5.6, 3.1, 6.4, 21.7) -> x
1/x
## [1] 0.09615385 0.17857143 0.32258065 0.15625000 0.04608295
y <- c(x, 0, x)
y
## [1] 10.4 5.6 3.1 6.4 21.7 0.0 10.4 5.6 3.1 6.4 21.7
v <- 2*x + y + 1
## Warning in 2 * x + y: longer object length is not a multiple of shorter object
## length
v
## [1] 32.2 17.8 10.3 20.2 66.1 21.8 22.6 12.8 16.9 50.8 43.5
max(x)
## [1] 21.7
min(x)
## [1] 3.1
range(x)
## [1] 3.1 21.7
length(x)
## [1] 5
sum(x)
## [1] 47.2
prod(x)
## [1] 25073.95
mean(x)
## [1] 9.44
sum(x)/length(x)
## [1] 9.44
var(x)
## [1] 53.853
sort(x)
## [1] 3.1 5.6 6.4 10.4 21.7
1:30
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
## [26] 26 27 28 29 30
2*1:15
## [1] 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30
n <- 10
1:n-1
## [1] 0 1 2 3 4 5 6 7 8 9
1:(n-1)
## [1] 1 2 3 4 5 6 7 8 9
30:1
## [1] 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6
## [26] 5 4 3 2 1
sqrt(-17+0i)
## [1] 0+4.123106i
30:1
## [1] 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6
## [26] 5 4 3 2 1
seq(2,10)
## [1] 2 3 4 5 6 7 8 9 10
seq(-5, 5, by=.2)
## [1] -5.0 -4.8 -4.6 -4.4 -4.2 -4.0 -3.8 -3.6 -3.4 -3.2 -3.0 -2.8 -2.6 -2.4 -2.2
## [16] -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8
## [31] 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6 3.8
## [46] 4.0 4.2 4.4 4.6 4.8 5.0
s3 <- seq(-5, 5, by=.2)
s3
## [1] -5.0 -4.8 -4.6 -4.4 -4.2 -4.0 -3.8 -3.6 -3.4 -3.2 -3.0 -2.8 -2.6 -2.4 -2.2
## [16] -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8
## [31] 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6 3.8
## [46] 4.0 4.2 4.4 4.6 4.8 5.0
s4 <- seq(length=51, from=-5, by=.2)
s4
## [1] -5.0 -4.8 -4.6 -4.4 -4.2 -4.0 -3.8 -3.6 -3.4 -3.2 -3.0 -2.8 -2.6 -2.4 -2.2
## [16] -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8
## [31] 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6 3.8
## [46] 4.0 4.2 4.4 4.6 4.8 5.0
s5 <- rep(x, times=5)
s5
## [1] 10.4 5.6 3.1 6.4 21.7 10.4 5.6 3.1 6.4 21.7 10.4 5.6 3.1 6.4 21.7
## [16] 10.4 5.6 3.1 6.4 21.7 10.4 5.6 3.1 6.4 21.7
s6 <- rep(x, each=5)
s6
## [1] 10.4 10.4 10.4 10.4 10.4 5.6 5.6 5.6 5.6 5.6 3.1 3.1 3.1 3.1 3.1
## [16] 6.4 6.4 6.4 6.4 6.4 21.7 21.7 21.7 21.7 21.7
is.na(x)
## [1] FALSE FALSE FALSE FALSE FALSE
temp <- x > 13
temp
## [1] FALSE FALSE FALSE FALSE TRUE
x < 13
## [1] TRUE TRUE TRUE TRUE FALSE
x <= 13
## [1] TRUE TRUE TRUE TRUE FALSE
x > 13
## [1] FALSE FALSE FALSE FALSE TRUE
x >= 13
## [1] FALSE FALSE FALSE FALSE TRUE
x == 13
## [1] FALSE FALSE FALSE FALSE FALSE
x != 13
## [1] TRUE TRUE TRUE TRUE TRUE
z <- c(1:3,NA)
ind <- is.na(z)
z
## [1] 1 2 3 NA
ind
## [1] FALSE FALSE FALSE TRUE
is.na(z)
## [1] FALSE FALSE FALSE TRUE
is.nan(0/0)
## [1] TRUE
labs <- paste(c("X","Y"), 1:10, sep="")
labs
## [1] "X1" "Y2" "X3" "Y4" "X5" "Y6" "X7" "Y8" "X9" "Y10"
y <- x[!is.na(x)]
y
## [1] 10.4 5.6 3.1 6.4 21.7
(x+1)[(!is.na(x)) & x>0] -> z
z
## [1] 11.4 6.6 4.1 7.4 22.7
c("x","y")[rep(c(1,2,2,1), times=4)]
## [1] "x" "y" "y" "x" "x" "y" "y" "x" "x" "y" "y" "x" "x" "y" "y" "x"
x[1]
## [1] 10.4
x[1:3]
## [1] 10.4 5.6 3.1
x[5]
## [1] 21.7
fruit <- c(5, 10, 1, 20)
names(fruit) <- c("orange", "banana", "apple", "peach")
fruit
## orange banana apple peach
## 5 10 1 20
lunch <- fruit[c("apple","orange")]
lunch
## apple orange
## 1 5
x[is.na(x)] <- 0
y[y < 0] <- -y[y < 0]
y <- abs(y)
y
## [1] 10.4 5.6 3.1 6.4 21.7
z <- 0:9
z
## [1] 0 1 2 3 4 5 6 7 8 9
mode(z)
## [1] "numeric"
length(z)
## [1] 10
digits <- as.character(z)
digits
## [1] "0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
d <- as.integer(digits)
d
## [1] 0 1 2 3 4 5 6 7 8 9
e <- numeric()
e
## numeric(0)
e[3] <- 17
e
## [1] NA NA 17
length(e)
## [1] 3
alpha <- 1:10
alpha
## [1] 1 2 3 4 5 6 7 8 9 10
alpha <- alpha[2 * 1:5]
alpha
## [1] 2 4 6 8 10
length(alpha) <- 3
alpha
## [1] 2 4 6
z <- 1:100
attributes(z)
## NULL
attr(z,"dim") <- c(10,10)
z
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 1 11 21 31 41 51 61 71 81 91
## [2,] 2 12 22 32 42 52 62 72 82 92
## [3,] 3 13 23 33 43 53 63 73 83 93
## [4,] 4 14 24 34 44 54 64 74 84 94
## [5,] 5 15 25 35 45 55 65 75 85 95
## [6,] 6 16 26 36 46 56 66 76 86 96
## [7,] 7 17 27 37 47 57 67 77 87 97
## [8,] 8 18 28 38 48 58 68 78 88 98
## [9,] 9 19 29 39 49 59 69 79 89 99
## [10,] 10 20 30 40 50 60 70 80 90 100
class(z)
## [1] "matrix" "array"
state <- c("tas", "sa", "qld", "nsw", "nsw", "nt", "wa", "wa",
"qld", "vic", "nsw", "vic", "qld", "qld", "sa", "tas",
"sa", "nt", "wa", "vic", "qld", "nsw", "nsw", "wa",
"sa", "act", "nsw", "vic", "vic", "act")
statef <- factor(state)
statef
## [1] tas sa qld nsw nsw nt wa wa qld vic nsw vic qld qld sa tas sa nt wa
## [20] vic qld nsw nsw wa sa act nsw vic vic act
## Levels: act nsw nt qld sa tas vic wa
levels(statef)
## [1] "act" "nsw" "nt" "qld" "sa" "tas" "vic" "wa"
incomes <- c(60, 49, 40, 61, 64, 60, 59, 54, 62, 69, 70, 42, 56,
61, 61, 61, 58, 51, 48, 65, 49, 49, 41, 48, 52, 46,
59, 46, 58, 43)
incmeans <- tapply(incomes, statef, mean)
incmeans
## act nsw nt qld sa tas vic wa
## 44.50000 57.33333 55.50000 53.60000 55.00000 60.50000 56.00000 52.25000
stdError <- function(x) sqrt(var(x)/length(x))
incster <- tapply(incomes, statef, stdError)
incster
## act nsw nt qld sa tas vic wa
## 1.500000 4.310195 4.500000 4.106093 2.738613 0.500000 5.244044 2.657536
ordered(state)
## [1] tas sa qld nsw nsw nt wa wa qld vic nsw vic qld qld sa tas sa nt wa
## [20] vic qld nsw nsw wa sa act nsw vic vic act
## Levels: act < nsw < nt < qld < sa < tas < vic < wa
factor(state, levels=c("act","nsw","nt","qld","sa","tas","vic","wa"),
ordered=TRUE)
## [1] tas sa qld nsw nsw nt wa wa qld vic nsw vic qld qld sa tas sa nt wa
## [20] vic qld nsw nsw wa sa act nsw vic vic act
## Levels: act < nsw < nt < qld < sa < tas < vic < wa
z <- 1:1500
dim(z) <- c(3,5,100)
dim(z)
## [1] 3 5 100
Z <- array(0,c(3,4,2))
Z
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0 0 0 0
## [2,] 0 0 0 0
## [3,] 0 0 0 0
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0 0 0 0
## [2,] 0 0 0 0
## [3,] 0 0 0 0
dim(Z)
## [1] 3 4 2
Z[1,1,1]
## [1] 0
Z[,,]
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 0 0 0 0
## [2,] 0 0 0 0
## [3,] 0 0 0 0
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 0 0 0 0
## [2,] 0 0 0 0
## [3,] 0 0 0 0
Z[1,,]
## [,1] [,2]
## [1,] 0 0
## [2,] 0 0
## [3,] 0 0
## [4,] 0 0
x <- array(1:20,dim=c(4,5))
x
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 5 9 13 17
## [2,] 2 6 10 14 18
## [3,] 3 7 11 15 19
## [4,] 4 8 12 16 20
i <- array(c(1:3,3:1),dim=c(3,2))
i
## [,1] [,2]
## [1,] 1 3
## [2,] 2 2
## [3,] 3 1
x[i]
## [1] 9 6 3
x[i] <- 0
x
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 5 0 13 17
## [2,] 2 0 10 14 18
## [3,] 0 7 11 15 19
## [4,] 4 8 12 16 20
A <- matrix(c(1,2,3,4),nrow=2)
B <- matrix(c(5,6,7,8),nrow=2)
A
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
B
## [,1] [,2]
## [1,] 5 7
## [2,] 6 8
A * B
## [,1] [,2]
## [1,] 5 21
## [2,] 12 32
A %*% B
## [,1] [,2]
## [1,] 23 31
## [2,] 34 46
t(A)
## [,1] [,2]
## [1,] 1 2
## [2,] 3 4
nrow(A)
## [1] 2
ncol(A)
## [1] 2
det(A)
## [1] -2
solve(A)
## [,1] [,2]
## [1,] -2 1.5
## [2,] 1 -0.5
x1 <- c(1,2,3)
x2 <- c(4,5,6)
cbind(x1,x2)
## x1 x2
## [1,] 1 4
## [2,] 2 5
## [3,] 3 6
rbind(x1,x2)
## [,1] [,2] [,3]
## x1 1 2 3
## x2 4 5 6
statefr <- table(statef)
statefr
## statef
## act nsw nt qld sa tas vic wa
## 2 6 2 5 4 2 5 4
table(statef)
## statef
## act nsw nt qld sa tas vic wa
## 2 6 2 5 4 2 5 4
incomef <- factor(cut(incomes,breaks=35+10*(0:7)))
table(incomef,statef)
## statef
## incomef act nsw nt qld sa tas vic wa
## (35,45] 1 1 0 1 0 0 1 0
## (45,55] 1 1 1 1 2 0 1 3
## (55,65] 0 3 1 3 2 2 2 1
## (65,75] 0 1 0 0 0 0 1 0