c("car")
wants <- wants %in% rownames(installed.packages())
has <-if(any(!has)) install.packages(wants[!has])
Lower types can be uniquely converted to higher types.
c(TRUE, FALSE, FALSE, TRUE)
tfVec <-as.numeric(tfVec)
[1] 1 0 0 1
as.complex(tfVec)
[1] 1+0i 0+0i 0+0i 1+0i
as.character(tfVec)
[1] "TRUE" "FALSE" "FALSE" "TRUE"
Higher types cannot be uniquely converted to lower types.
as.logical(c(-1, 0, 1, 2))
[1] TRUE FALSE TRUE TRUE
as.numeric(as.complex(c(3-2i, 3+2i, 0+1i, 0+0i)))
Warning: imaginäre Teile verworfen in Umwandlung
[1] 3 3 0 0
as.numeric(c("21", "3.141", "abc"))
Warning: NAs durch Umwandlung erzeugt
[1] 21.000 3.141 NA
c(10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
vec <-rev(vec)
[1] 20 19 18 17 16 15 14 13 12 11 10
c(10, 12, 1, 12, 7, 16, 6, 19, 10, 19)
vec <-sort(vec)
[1] 1 6 7 10 10 12 12 16 19 19
order(vec, decreasing=TRUE)) (idxDec <-
[1] 8 10 6 2 4 1 9 5 7 3
vec[idxDec]
[1] 19 19 16 12 12 10 10 7 6 1
sort(c("D", "E", "10", "A", "F", "E", "D", "4", "E", "A"))
[1] "10" "4" "A" "A" "D" "D" "E" "E" "E" "F"
set.seed(123)
c("red", "green", "blue", "yellow", "black")
myColors <- sample(myColors, length(myColors), replace=FALSE)) (randCols <-
[1] "green" "yellow" "black" "blue" "red"
3
P <- c(4, 3, 5)
Nj <- rep(1:P, Nj)) (IV <-
[1] 1 1 1 1 2 2 2 3 3 3 3 3
sample(IV, length(IV), replace=FALSE)) (IVrand <-
[1] 1 2 3 2 1 2 1 3 3 3 1 3
c(18, 11, 15, 20, 19, 10, 14, 13, 10, 10)
x <- length(x)
N <- 3
P <-sample(1:N, N, replace=FALSE) %% P) + 1 (
[1] 2 1 3 2 3 2 1 3 1 2
rep(c("red", "green", "blue"), 10)
vec <-sample(vec, 5, replace=FALSE)
[1] "blue" "blue" "green" "blue" "blue"
library(car)
some(vec, n=5)
[1] "red" "blue" "green" "blue" "blue"
seq(1, length(vec), by=10)
selIdx1 <- vec[selIdx1]
[1] "red" "green" "blue"
rbinom(length(vec), size=1, prob=0.1) == 1
selIdx2 <- vec[selIdx2]
character(0)
c(18, 20, 30, 24, 23, 21)
age <-/10 age
[1] 1.8 2.0 3.0 2.4 2.3 2.1
/2) + 5 (age
[1] 14.0 15.0 20.0 17.0 16.5 15.5
c(3, 4, 5, 6)
vec1 <- c(-2, 2, -2, 2)
vec2 <-*vec2 vec1
[1] -6 8 -10 12
c(10, 100, 1000, 10000)
vec3 <-+ vec2) / vec3 (vec1
[1] 1e-01 6e-02 3e-03 8e-04
c(2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24)
vec1 <- c(2, 4, 6, 8, 10)
vec2 <-c(length(age), length(vec1), length(vec2))
[1] 6 12 5
*age vec1
[1] 36 80 180 192 230 252 252 320 540 480 506 504
*age vec2
Warning in vec2 * age: Länge des längeren Objektes
ist kein Vielfaches der Länge des kürzeren Objektes
[1] 36 80 180 192 230 42
(age - mean(age)) / sd(age)) (zAge <-
[1] -1.1166106 -0.6380632 1.7546739 0.3190316 0.0797579 -0.3987895
scale(age)) (zAge <-
[,1]
[1,] -1.1166106
[2,] -0.6380632
[3,] 1.7546739
[4,] 0.3190316
[5,] 0.0797579
[6,] -0.3987895
attr(,"scaled:center")
[1] 22.66667
attr(,"scaled:scale")
[1] 4.179314
as.vector(zAge)
[1] -1.1166106 -0.6380632 1.7546739 0.3190316 0.0797579 -0.3987895
15
newSd <- 100
newMean <- (as.vector(zAge)*newSd) + newMean) (newAge <-
[1] 83.25084 90.42905 126.32011 104.78547 101.19637 94.01816
mean(newAge)
[1] 100
sd(newAge)
[1] 15
rank(c(3, 1, 2, 3))
[1] 3.5 1.0 2.0 3.5
c(1.78, 1.91, 1.89, 1.83, 1.64)
height <- c(65, 89, 91, 75, 73)
weight <- weight / (height^2)) (bmi <-
[1] 20.51509 24.39626 25.47521 22.39541 27.14158
c(FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE)
quest1 <- c(TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE)
quest2 <- c(TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, FALSE, FALSE)
quest3 <- quest1 + quest2 + quest3) (sumVar <-
[1] 2 1 1 2 1 3 1 1
try(detach(package:car))
R markdown - markdown - R code - all posts