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

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 45

is.vector(x)
## [1] TRUE

y <- 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] 8

x[-2]
## [1] 7 10 45

x[c(1,3)]
## [1] 7 10

x[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 45

x[c(1,3)] <- 20
x
## [1] 20 0 20 45

x[-4] <- 100
x
## [1] 100 100 100 45

x[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 FALSE

weekly.hours = vector(length=5,mode = "character")
weekly.hours
## [1] "" "" "" "" ""

weekly.hours = vector(length=5,mode = "numeric")
weekly.hours
## [1] 0 0 0 0 0

weekly.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.974859

rbinom(n=5,size = 10,prob = 0.5)
## [1] 3 2 5 4 5

runif(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 0

x * 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 37

x^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 TRUE

identical(x, -y)
## [1] TRUE

identical(c(0.5-0.3,0.3-0.1), c(0.3-0.1,0.5-0.3))
## [1] FALSE

all.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:234

summary(x)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.00 59.25 117.50 117.50 175.75 234.00

any(x>100)
## [1] TRUE

all(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] NA

sd(x)
## [1] NA

mean(x,na.rm = T)
## [1] 117.5

summary(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 45

places = 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.kler.cn/news/368772.html

相关文章:

  • RabbitMQ 确认模式(Acknowledgements Mode)详解
  • Python的协程与传统的线程相比,是否能更有效地利用计算资源?在多大程度上,这种效率是可测量的?如何量化Python协程的优势|协程|线程|性能优化
  • Java | Leetcode Java题解之第513题找树左下角的值
  • C++算法练习-day15——1.两数之和
  • Java | ReentrantLock 锁和 synchronized 锁的区别和共同特点是什么?
  • IntelliJ IDEA 查看类class的结构Structure轮廓outline窗口, 快捷键是Alt+7
  • MySQL之数据库设计
  • ReactNative0.76版本发布,默认开启新架构
  • 【K8S系列】Kubernetes Service 基础知识 详细介绍
  • java和嵌入式现在哪个好?
  • 【深入理解SpringCloud微服务】Sentinel实战与原理剖析
  • 重修设计模式-行为型-迭代器模式
  • 了解光耦合器输入输出关系---腾恩科技
  • 区块链国赛题目--食品溯源(模块三)
  • 租房管理智能化:Spring Boot系统开发指南
  • VMware16去虚拟化 过CF 理论过TX游戏 WIN10过检测虚拟机
  • 微信小程序SSL证书怎么选择?
  • 通过 SYSENTER/SYSEXIT指令来学习系统调用
  • 《链表篇》---环形链表
  • 数据挖掘(二)
  • Typora一款极简Markdown文档编辑器和阅读器,实时预览,序列号生成!免费!最新可用!
  • CentOS 自启动某个应用
  • IP协议详解:报头格式、主机定位、转发流程、网段划分与路由机制
  • vue通过JSON文件生成WPML文件源码
  • 关于 API
  • 【leetcode】动态规划