当前位置: 首页 > 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
  • 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.mrgr.cn/news/57626.html

相关文章:

  • ESP32 S3 语音识别 语音唤醒程序流程
  • C++:多态(用法篇)
  • 无人机之自主飞行关键技术篇
  • 软件测试与软件缺陷的基础知识
  • 详细且系统的Spring Boot应用开发
  • 如何设置Page Cache的大小为默认值
  • 基础数据结构——队列(双端队列,优先级队列,阻塞队列)
  • 【MySQL】C语言连接MySQL数据库3——事务操作和错误处理API
  • C++中指针类型、引用类型、值类型
  • 面试必备:RabbitMQ与Kafka核心知识点总结
  • 使用 SpaCy 和 NLTK 进行文本处理与切片详解
  • 中酱集团:黑松露酱油,天然配方定义健康生活
  • 【golang】学习文档整理
  • 11 怎么给字符串字段加索引?
  • js 基础补充3
  • 【面试题】如果 Redis 遇到 Hash 冲突了该怎么处理?
  • 代码随想录-哈希表-快乐数
  • 安装报错解决:No module named ‘quaternion‘
  • 无线路由器设置成交换机(补充)
  • kotlin等待异步任务完成
  • 植入感体感游戏
  • 嵌入式编程守则
  • 计算机xapofx1_5.dll丢失怎么办,分享5种有效的解决方法
  • 基于SpringBoot的在线拍卖系统【源码+论文】
  • 4466 最长连续重复字符(longest)
  • 数据库中DDL、DML、DCL的区别是什么