当前位置: 首页 > news >正文

R语言笔记(二):向量

文章目录

  • 一、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<-3x<-c(3) 可以看做是等价的
  • 1:5 is shorthand for c(1,2,3,4,5), and so on

二、Indexing vectors

  • x[1] would be the first element, x[2] the second element, and x[-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(), and sum() return single numbers
  • sort() returns a new vector
  • summary() gives a five-number summary of numerical vectors
  • any() and all() 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
http://www.lryc.cn/news/470257.html

相关文章:

  • 信息安全工程师(71)隐私保护技术与应用
  • 层和块学习
  • Zookeeper面试整理-源码及实现细节
  • 岭回归的MATLAB步骤
  • 智能指针(unique_ptr,shared_ptr,weak_ptr)
  • Sql执行较慢的排查方式
  • CesiumJS 案例 P6:添加图片图层、添加图片图层并覆盖指定区域
  • Python画笔案例-094 绘制 神奇彩条动画
  • javaScript整数反转
  • Zookeeper面试整理-故障排查和调试
  • PG数据库之索引详解
  • springboot项目测试环境构建出的依赖包比本地构建出的依赖包多
  • 温湿度传感器(学习笔记上)
  • sv标准研读第十九章-功能覆盖率
  • 图集短视频去水印云函数开发实践——小红书
  • Uni-App-03
  • 解决 VScode 每次打开都是上次打开的文件问题
  • redis高级篇之skiplist跳表 第164节答疑
  • Java 线程池:深入理解与高效应用
  • week08 zookeeper多种安装与pandas数据变换操作-new
  • js构造函数和原型对象,ES6中的class,四种继承方式
  • 电脑连接海康相机并在PictureBox和HWindowControl中分别显示。
  • 直播系统源码技术搭建部署流程及配置步骤
  • Spring+ActiveMQ
  • Linux 常用命令总汇
  • fmql之Linux RTC
  • Flask-SocketIO 简单示例
  • Vue 3 的组件式开发(2)
  • python 爬虫 入门 四、线程,进程,协程
  • cloak斗篷伪装下的独立站