vector
: basic data structure in Rnumeric
, character
, logical
c()
function to combine elements to a vectorcharacter
: elements are specified in quotes: "hello"
logical
: keywords TRUE
or FALSE
numeric
: regular numbersc(3+4, 2^3, sqrt(123), exp(2))
[1] 7.000000 8.000000 11.090537 7.389056
c(TRUE, FALSE, FALSE)
[1] TRUE FALSE FALSE
c("hi", "by")
[1] "hi" "by"
m <- seq(1, 12)
length(m)
[1] 12
c()
function: c(1, 2, 12, 3)
rep()
function: rep("A", times = 4)
seq()
function seq(from = 1, to = 4, by = 0.25)
c()
to combine values to a vector of the same data type.rep()
to repeat the values of a given vectorseq()
to generate sequences with fixed lengthx:y
short for seq(x, y, by = 1)
as.numeric()
, is.numeric()
, as.character()
, is.character()
, … one can test and cast an object from one type to anotheras.numeric("4")
[1] 4
is.logical(0)
[1] FALSE
is.logical(TRUE)
[1] TRUE
month <- 1:12
month
[1] 1 2 3 4 5 6 7 8 9 10 11 12
names(month) <- c("Jan", "Feb", "Mär", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez")
month
Jan Feb Mär Apr Mai Jun Jul Aug Sep Okt Nov Dez
1 2 3 4 5 6 7 8 9 10 11 12
rnorm()
random numbers from the normal distributionrbinom()
- random numbers from the binomial distributionrunif()
- uniform distributionrt()
- t distributionsample()
- sample values with or without replacement from a vector<-
: \(name \times value = object\)3^2
[1] 9
-3+5
[1] 2
3*7
[1] 21
9/3
[1] 3
4 < 5
[1] TRUE
3 != 4
[1] TRUE
5 == 2
[1] FALSE
"a" < "b"
[1] TRUE
"hallo" == 10
[1] FALSE
TRUE | FALSE
[1] TRUE
TRUE & FALSE
[1] FALSE
!TRUE
[1] FALSE
y <- 10
x <- 2
x
[1] 2
numbers <- c(1, 2, 5, -100, 23, 43)
sqrt(numbers)
Warning in sqrt(numbers): NaNs produced
[1] 1.000000 1.414214 2.236068 NaN 4.795832 6.557439
numbers < 0
[1] FALSE FALSE FALSE TRUE FALSE FALSE
NA
val <- c(3, 5, 2, NA)
sum(val)
[1] NA
sum(val, na.rm = T)
[1] 10
is.na(val)
[1] FALSE FALSE FALSE TRUE
sum(is.na(val))
[1] 1