**
vector
*
1 vector R is the most basic type of
type 2. A vector elements must be the same , comprising: numeric, logical, string, complex-valued
mode () - the type of object
length () - length of the object (refer to the number of elements)
NA - missing values (Not availabel)
NaN3 - not represented by a digital value (not a number)
a=“statistic”;b=TRUE;c=1i
mode(a);mode(b);mode(c )
[1] “character”
[1] “logical”
[1] “complex”
length(a);length(b);length(c )
[1] 1
[1] 1
[1] 1
z=c(‘zx’,‘df’)
length(z)
[1] 2
sqrt (-2)
[. 1] NaN3
Warning Message:
the In sqrt (-2): produced NaNs
Establish vectors
Establish the function value vector
seq () - If the vector (SEQ) with a more simple rule
rep () - If the vector (SEQ) having a more complex rule of
sequence () - If the vector (Sequence) is an integer sequence
C ( ) - If the vector (sequence) no law
seq(1,10,by=0.5) # =seq(from=1,to=10,by=0.5)
[1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5
[15] 8.0 8.5 9.0 9.5 10.0
seq(1,10,length=21) # = seq(1,10,length.out=21)
[1] 1.00 1.45 1.90 2.35 2.80 3.25 3.70 4.15 4.60 5.05 5.50 5.95
[13] 6.40 6.85 7.30 7.75 8.20 8.65 9.10 9.55 10.00
rep(2:5,2) # = rep(2:5, times=2)
[1] 2 3 4 5 2 3 4 5
rep(2:5, c(1,2,3,4))
[1] 2 3 3 4 4 4 5 5 5 5
rep(1:3, times = 4, each = 2)
[1] 1 1 2 2 3 3 1 1 2 2 3 3 1 1 2 2 3 3 1 1 2 2 3 3
sequence(5)
[1] 1 2 3 4 5
sequence(2:5)
[1] 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Vector calculation
Value vector
5 + c(4,7,17)
[1] 9 12 22
5 * c(4,7,17)
[1] 20 35 85
c(-1,3,-17) + c(4,7,17)
[1] 3 10 0
c(2,4,5)^2
[1] 4 16 25
sqrt(c(2,4,25))
[1] 1.414214 2.000000 5.000000
mean(c(2,4,9))
[1] 5
var(c(2,4,9))
[1] 13
Logic vector
x <- c(10.4, 5.6, 3.1, 6.4, 21.7)
x>13
[1] FALSE FALSE FALSE FALSE TRUE
Logic vectors may be used in a normal operation, at which point it is converted to a vector of numbers, FALSE as 0, TRUE as 1
(x>13) + 1
[1] 1 1 1 1 2
And Factor
converting the character into a vector factor
levels - indicates the level of Factor
a <- c(“green”, “blue”, “green”, “yellow”)
factor(a)
[1] green blue green yellow
Levels: blue green yellow
To a horizontal factor named
levels(a) <- c(“low”, “middle”, “high”)
a
[1] “green” “blue” “green” “yellow”
attr(,“levels”)
[1] “low” “middle” “high”
Converting the vector into a numerical factor
b <- c(1,2,3,1)
factor(b)
[1] 1 2 3 1
Levels: 1 2 3
Factor converting the character into a numerical factor
ff <- factor(c(“A”, “B”, “C”), labels=c(1,2,3))
ff
[1] 1 2 3
Levels: 1 2 3
The conversion factor is the numerical character Factor
ff <- factor(1:3, labels=c (“A”, “B”, " C "))
ff
[1] A B C
Levels: A B C
gl (k, n, label, length) produce a regular sequence of Factor
k - number of levels
n - number of repetitions of each horizontal
label - Specifies the name of each horizontal factor (optional)
length - Specifies the number of generated data (may selected)
gl(2, 3, label=c (“Male”, “Female”),length=10)
[1] Male Male Male Female Female Female Male Male Male Female
Levels: Male Female
Cycle rule vector operations
1:2+1:4
[1] 2 4 4 6
. 1: +. 1. 4:. 7
[. 1] 2. 8. 6. 8. 6 10. 4
Warning Message:
the In. 1: +. 1. 4:. 7: integer multiple long object length shorter than the length of the object
Vector index to the subset of the extracted (element)
The subscript n - extraction vector element corresponding
negative subscript - remove the element in the vector corresponding to
the logical operation - to extract the value of the vector elements in the element satisfies the condition
NOTE: subscript R 1 from the start of the vector, this is consistent with the usual statistical or mathematical software
x <- c(42, 7, 64, 9)
x[1]
[1] 42
x[c(2,4)]
[1] 7 9
x[-2]
[1] 42 64 9
x> 10 # 10 value is greater than the logical value of the element
[. 1] TRUE FALSE TRUE FALSE
X [X> 10] is greater than the value of the element # 10
[. 1] 42 is 64
X [X <X 40 &> 10]
numeric (0)
Generating random numbers (0, 1) 10 of uniform distribution
and <- runif (10, min = 0, max = 1)
and
[1] 0.6215151 0.8524313 0.5984349 0.5263928 0.2448307 0.5444761 0.5032350
[8] 0.8018147 0.7914064 0.7655385
The number of sum (y <0.5) # 0.5 is smaller than the element
[. 1]. 1
length (Y [Y <0.5]) is equivalent to the above #
[. 1]. 1
SUM (Y [Y <0.5]) less than 0.5 # and element values
[1] .2448307
SD (Y [Y <0.8]) less than the standard value of # 0.8 elements constituting the difference samples
[1] 0.1704212
Follow-up: important data structure - Matrix