R语言笔记(二):向量

news/2024/10/27 23:12:29/

文章目录

  • 一、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.ppmy.cn/news/1542461.html

相关文章

【笔记】apt源设置为阿里云源

目录 1. 备份现有的源列表&#xff08;可选&#xff0c;但推荐&#xff09;&#xff1a;2. 编辑 sources.list 文件&#xff1a;3. 替换内容为阿里源&#xff1a;4. 保存并退出&#xff1a;5. 更新软件包索引&#xff1a; 在 Ubuntu 系统中&#xff0c;将 APT 源设置为阿里云源…

【C++单调栈 贡献法】907. 子数组的最小值之和|1975

本文涉及的基础知识点 C单调栈 LeetCode907. 子数组的最小值之和 给定一个整数数组 arr&#xff0c;找到 min(b) 的总和&#xff0c;其中 b 的范围为 arr 的每个&#xff08;连续&#xff09;子数组。 由于答案可能很大&#xff0c;因此 返回答案模 109 7 。 示例 1&#x…

Word 删除空白页:亲测有效的方法

# Word 删除空白页&#xff1a;亲测有效的方法 在使用Microsoft Word编写文档时&#xff0c;有时会遇到空白页的问题&#xff0c;这可能会影响文档的排版和可读性。以下是几种有效的方法来删除Word文档中的空白页。 ## 一、检查段落标记和分页符 ### 1. 显示/隐藏段落标记 -…

深入理解JAVA虚拟机(四)

介绍对象Mark Word里面锁状态的流转 线程真正有四个状态&#xff1a;无锁状态 &#xff08;标识01&#xff0c;是否偏向0&#xff09;、偏向锁&#xff08;标识01、是否偏向1&#xff09;、轻量级锁&#xff08;标识00&#xff09;、重量级锁&#xff08;标识&#xff1a;10&a…

耦合回路网络

目录 去耦 阻抗 初级回路对次级回路的影响 次级回路对初级回路的影响 对比 物理意义 讨论: 谐振时 ​编辑 谐振 耦合回路谐振分类 部分谐振 复谐振 全谐振 要研究耦合回路 , 就要先进行去耦的工作 去耦 对于去耦的方法 , 总共列出两个方程 , 初级回路与次级回路的方程求解…

电子电气架构 --- 电气系统工程

我是穿拖鞋的汉子,魔都中坚持长期主义的汽车电子工程师。 老规矩,分享一段喜欢的文字,避免自己成为高知识低文化的工程师: 屏蔽力是信息过载时代一个人的特殊竞争力,任何消耗你的人和事,多看一眼都是你的不对。非必要不费力证明自己,无利益不试图说服别人,是精神上的节…

【AI大模型】深入解析 存储和展示地理数据(.kmz)文件格式:结构、应用与项目实战

文章目录 1. 引言2. 什么是 .kmz 文件&#xff1f;2.1 .kmz 文件的定义与用途2.2 .kmz 与 .kml 的关系2.3 常见的 .kmz 文件使用场景 3. .kmz 文件的内部结构3.1 .kmz 文件的压缩格式3.2 解压缩 .kmz 文件的方法3.3 .kmz 文件的典型内容3.4 .kml 文件的结构与主要元素介绍 4. 深…

AudioSegment 提高音频音量 - python 实现

一些采集的音频声音音量过小可以通过 AudioSegment 实现音量增强。 按照 python 库&#xff1a; pip install AudioSegment 代码具体实现&#xff1a; #-*-coding:utf-8-*- # date:2024-10 # Author: DataBall - XIAN # Function: 音频增加音量import os from pydub import …