numeric(4)
[1] 0 0 0 0
character(3)
[1] "" "" ""
logical(5)
[1] FALSE FALSE FALSE FALSE FALSE
Numeric vectors
c(18, 20, 30, 24, 23, 21)) (age <-
[1] 18 20 30 24 23 21
c(27, 21, 19)
addAge <- c(age, addAge)) (ageNew <-
[1] 18 20 30 24 23 21 27 21 19
append(age, c(17, 31))
[1] 18 20 30 24 23 21 17 31
Character vectors
c("Z", "Y", "X")
charVec1 <- c(charVec1, "T", "S", "R")) (charVec2 <-
[1] "Z" "Y" "X" "T" "S" "R"
c(1, 2, 3)] LETTERS[
[1] "A" "B" "C"
c(5, 9, 13)] letters[
[1] "e" "i" "m"
c("ipsum", "dolor", "sit")) (chars <-
[1] "ipsum" "dolor" "sit"
Information about vectors
length(age)
[1] 6
length(chars)
[1] 3
nchar(chars)
[1] 5 5 3
4] age[
[1] 24
4] <- 22
age[ age
[1] 18 20 30 22 23 21
Get and change the last element
age[length(age)]) (ageLast <-
[1] 21
length(age) + 1] age[
[1] NA
A vector does not need a name for getting one of its values
c(11, 12, 13, 14)[2]
[1] 12
Get elements
c(1, 2, 4)
idx <- age[idx]
[1] 18 20 22
c(3, 5, 6)] age[
[1] 30 23 21
c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6)] age[
[1] 18 18 20 20 30 30 22 22 23 23 21 21
c(4, NA, 1)] age[
[1] 22 NA 18
Change elements
c(17, 30, 25)
age[idx] <- age
[1] 17 30 30 25 23 21
-3] age[
[1] 17 30 25 23 21
c(-1, -2, -4)] age[
[1] 30 23 21
-c(1, 2, 4)] age[
[1] 30 23 21
-idx] age[
[1] 30 23 21
Also see help(Extract)
"word"
charVec4 <- c(10, 20, 30)
numVec <- c(charVec4, numVec)) (combVec <-
[1] "word" "10" "20" "30"
mode(combVec)
[1] "character"
c(elem1="first", elem2="second")) (namedVec1 <-
elem1 elem2
"first" "second"
"elem1"] namedVec1[
elem1
"first"
c(val1=10, val2=-12, val3=33)) (namedVec2 <-
val1 val2 val3
10 -12 33
names(namedVec2)
[1] "val1" "val2" "val3"
names(namedVec2) <- c("A", "B", "C")
namedVec2
A B C
10 -12 33
c(10, 20, 30, 40, 50)
vec <- vec[c(-4, -5)]
vec <- vec
[1] 10 20 30
c(1, 2, 3, 4, 5)
vec <-length(vec) <- 3
vec
[1] 1 2 3
c(17, 30, 30, 24, 23, 21)
age <-< 24 age
[1] TRUE FALSE FALSE FALSE TRUE TRUE
c(2, 4, 8)
x <- c(3, 4, 5)
y <-== y x
[1] FALSE TRUE FALSE
< y x
[1] TRUE FALSE FALSE
Information about elements satisfying some condition
age > 30
res <-any(res)
[1] FALSE
any(age < 18)
[1] TRUE
all(x == y)
[1] FALSE
age < 24
res <-sum(res)
[1] 3
which(age < 24)
[1] 1 5 6
length(which(age < 24))
[1] 3
Checking for equality of vectors
c(4, 5, 6)
x <- c(4, 5, 6)
y <- c(1, 2, 3)
z <-all.equal(x, y)
[1] TRUE
all.equal(y, z)
[1] "Mean relative difference: 0.6"
isTRUE(all.equal(y, z))
[1] FALSE
<= 20) | (age >= 30) (age
[1] TRUE TRUE TRUE FALSE FALSE FALSE
> 20) & (age < 30) (age
[1] FALSE FALSE FALSE TRUE TRUE TRUE
c(TRUE, FALSE, TRUE, TRUE, FALSE, TRUE)] age[
[1] 17 30 24 21
(age <= 20) | (age >= 30)) (idx <-
[1] TRUE TRUE TRUE FALSE FALSE FALSE
age[idx]
[1] 17 30 30
>= 30) | (age <= 20)] age[(age
[1] 17 30 30
c(TRUE, FALSE)] age[
[1] 17 30 23
c(TRUE, FALSE, TRUE, FALSE, TRUE, FALSE)] age[
[1] 17 30 23
Problem:
c(-3, 2, 0, NA, -7, 5)
vecNA <- vecNA > 0) (logIdx <-
[1] FALSE TRUE FALSE NA FALSE TRUE
vecNA[logIdx]
[1] 2 NA 5
Solution:
which(logIdx)) (numIdx <-
[1] 2 6
vecNA[numIdx]
[1] 2 5
seq(along=logIdx) %in% numIdx
[1] FALSE TRUE FALSE FALSE FALSE TRUE
R markdown - markdown - R code - all posts