文章目录
- 一、Data structure: vectors
- 二、Indexing vectors
- 三、Re-assign values to vector elements
- 四、Generic function for vectors
- 五、Vector of random samples from a distribution
- 六、Vector arithmetic
- 七、Recycling
- 八、Element-wise comparisons of vectors
- 九、Comparisons across whole vectors
- 十、Functions on vectors
- 十一、Vectors with NA
- 十二、Element accession with condition
- 十三、Named elements
一、Data structure: vectors
- A data structure is a grouping of related data values into an object
- A vector is a sequence of values,
all of the same type
x = c(7, 8, 10, 45)
x
## [1] 7 8 10 45is.vector(x)
## [1] TRUEy <- 1:10
y
## [1] 1 2 3 4 5 6 7 8 9 10
- The
c()
function returns a vector containing all its arguments in specified order - 这里的c就是 combine 或 concatenate 的意思
x<-3
与x<-c(3)
可以看做是等价的1:5
is shorthand forc(1,2,3,4,5)
, and so on
二、Indexing vectors
x[1]
would be the first element,x[2]
the second element, andx[-2]
is a vector containing all but the second element
x = c(7, 8, 10, 45)x[2]
## [1] 8x[-2]
## [1] 7 10 45x[c(1,3)]
## [1] 7 10x[c(T,F,T,F)]
## [1] 7 10
三、Re-assign values to vector elements
x = c(7, 8, 10, 45)x[2] <- 0
x
## [1] 7 0 10 45x[c(1,3)] <- 20
x
## [1] 20 0 20 45x[-4] <- 100
x
## [1] 100 100 100 45x[c(T,F,T,F)] <- -100
x
## [1] -100 100 -100 45
四、Generic function for vectors
vector(length=n)
returns an empty
vector of length n; helpful for filling things up later
weekly.hours = vector(length=5)
weekly.hours
## [1] FALSE FALSE FALSE FALSE FALSEweekly.hours = vector(length=5,mode = "character")
weekly.hours
## [1] "" "" "" "" ""weekly.hours = vector(length=5,mode = "numeric")
weekly.hours
## [1] 0 0 0 0 0weekly.hours[5] = 8
weekly.hours
##[1]0 0 0 0 8
五、Vector of random samples from a distribution
Samples from normal distribution: rnorm
; binomial distribution :rbinom
; uniform distribution: runif
rnorm(n=5,mean=5,sd=3)
## [1] 5.402176 6.584742 5.557738 1.758993 2.974859rbinom(n=5,size = 10,prob = 0.5)
## [1] 3 2 5 4 5runif(n=5,min = 0,max = 10)
## [1] 7.2681322 2.7109939 2.9201373 0.4673917 9.4665859
六、Vector arithmetic
Arithmetic operator apply to vectors in a “component-wise” fashion
x = c(7, 8, 10, 45)
y = c(-7, -8, -10, -45)
x + y
## [1] 0 0 0 0x * y
## [1] -49 -64 -100 -2025
七、Recycling
Recycling repeat elements in shorter vector when combined with a longer one
x = c(7, 8, 10, 45)
x + c(-7,-8)
## [1] 0 0 3 37x^c(1,0,-1,0.5)
## [1] 7.000000 1.000000 0.100000 6.708204
Single numbers are vectors of length 1 for purposes of recycling:
2 * x
## [1] 14 16 20 90
八、Element-wise comparisons of vectors
x = c(7, 8, 10, 45)
x > 9
## [1] FALSE FALSE TRUE TRUE
Logical operators also work elementwise:
(x > 9) & (x < 20)
## [1] FALSE FALSE TRUE FALSE
九、Comparisons across whole vectors
To compare whole vectors, best to use identical()
or all.equal()
:
x = c(7, 8, 10, 45)
y = -c(7, 8, 10, 45)x == -y
## [1] TRUE TRUE TRUE TRUEidentical(x, -y)
## [1] TRUEidentical(c(0.5-0.3,0.3-0.1), c(0.3-0.1,0.5-0.3))
## [1] FALSEall.equal(c(0.5-0.3,0.3-0.1), c(0.3-0.1,0.5-0.3))
## [1] TRUE
十、Functions on vectors
Many functions can take vectors as arguments:
mean()
,median()
,sd()
,var()
,max()
,min()
,
length()
, andsum()
return single numberssort()
returns a new vectorsummary()
gives a five-number summary of numerical vectorsany()
andall()
are useful on Boolean vectors
x <- 1:234summary(x)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.00 59.25 117.50 117.50 175.75 234.00any(x>100)
## [1] TRUEall(x>100)
## [1] FALSE
十一、Vectors with NA
The existence of NA
will influence the output of some functions
x <- c(1:234,NA)mean(x)
## [1] NAsd(x)
## [1] NAmean(x,na.rm = T)
## [1] 117.5summary(x)
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 1.00 59.25 117.50 117.50 175.75 234.00 1
十二、Element accession with condition
Return elements with values greater than 9
x = c(7, 8, 10, 45)x[x > 9]
## [1] 10 45places = which(x > 9)
places
## [1] 3 4
十三、Named elements
We can give names to elements of vectors, and index vectors accordingly
names(x) = c("v1","v2","v3","fred")names(x)
## [1] "v1" "v2" "v3" "fred"x[c("fred","v1")]
## fred v1
## 45 7