首页 > 其他分享 >R语言笔记Vector(二)

R语言笔记Vector(二)

时间:2024-10-23 09:48:23浏览次数:3  
标签:vector 10 45 FALSE 语言 ## vectors 笔记 Vector

文章目录


一、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
  • 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

标签:vector,10,45,FALSE,语言,##,vectors,笔记,Vector
From: https://blog.csdn.net/BingJH2018212666/article/details/143024294

相关文章

  • Vue3 学习笔记(三)Vue3 项目打包及目录结构说明
    一、Vue3项目打包我们来回顾一下前面的调试运行命令:npmrundev执行后输出:VITEv5.4.9readyin483ms➜Local:http://localhost:5173/➜Network:use--hosttoexpose➜VueDevTools:Openhttp://localhost:5173/__devtools__/asasepa......
  • 字符串哈希 学习笔记
    两种哈希的表示方式。设\(s_i\)为字符串内第\(i\)位,\(h_i\)表示字符串内\([1,i]\)的哈希值,\(p\)为模数,那么第一种哈希方式是:\(h_i=h_{i-1}*p+s_i\),即把\(h_i\)当作一个\(p\)进制数,加入\(s_i\)时在数的末尾。\(h_i=h_{i-1}+s_i*p^{i-1}\),即是在开头加入\(s_i\)......
  • 为什么有些人一拿到新笔记本就直接重装系统?看完就明白了
    前言前段时间有个小伙伴买了一台笔记本,用了一段时间之后发现新电脑并不是那么好用。明明买了很贵的笔记本电脑(Windows11系统),但为啥就是偶尔卡顿呢?先来说说这个电脑的配置是怎么样的:i5-13500H16GBDDR4500GBSSD如果这台电脑用来日常办公已经是绰绰有余了,但是为什么......
  • 一图总结sql语言的最常用知识
     一,五大类sql语言DDLDataDefinitionLanguage,数据定义语言,用于定义不同的数据字段、数据库、表、列、索引。如:create、drop、alter等DMLDataManipulationLanguage,数据操作语言,用于添加、删除、修改、查询数据的完整性。如:insert、update、delete等DQLDataQuery......
  • 关于我、重生到500年前凭借C语言改变世界科技vlog.8——函数递归
    文章目录1.递归的介绍2.递归的限制条件3.递归实战应用3.1求n的阶乘3.2顺序打印一个整数的每一位4.递归与迭代5.递归经典问题的拓展希望读者们多多三连支持小编会继续更新你们的鼓励就是我前进的动力!1.递归的介绍在vlog.2的printf函数的返回值举例中,我们使......
  • 【笔记】CSE 365 - Fall 2024之Talking Web(pwn.college)
    【入门笔记】CSE365-Fall2024之TalkingWeb(pwn.college)先看完level1 使用curl发送HTTP请求curl是一个用于在命令行中与网络进行交互的工具,支持多种协议,如HTTP、HTTPS、FTP等。它可以用来发送GET、POST等请求,下载文件,上传数据,甚至处理API调用。由于其灵活性和广......
  • 《使用Gin框架构建分布式应用》阅读笔记:p108-p126
    《用Gin框架构建分布式应用》学习第8天,p108-p126总结,总计18页。一、技术总结1.Redisevictionpolicy(1)什么是evictionpolicy?Theevictionpolicydetermineswhathappenswhenadatabasereachesitsmemorylimit.(2)配置示例在redis.conf中配置。maxmemory-policy......
  • 计算机网络 | 第一章 认识计算机网络 | 26王道考研自用笔记
    一、认识计算机网络1.1计算机网络的定义与分类1.1.1计算机网络的定义计算机网络是一个将众多分散的、自治的计算机系统,通过通信设备与线路连接起来,由功能完善的软件实现资源共享和信息传递的系统。计算机网络(computernetworking)、互连网(internet)和互连网(Internet)的......
  • R语言机器学习系列教程大纲
    R语言机器学习算法实战系列(一)XGBoost算法+SHAP值(eXtremeGradientBoosting)R语言机器学习算法实战系列(二)SVM算法+重要性得分(SupportVectorMachine)R语言机器学习算法实战系列(三)lightGBM算法+SHAP值(LightGradientBoostingMachine)R语言机器学习算法实战系列(四)随机森林算法......
  • 环论笔记(1)
    环设\(R\)是赋予了加法和乘法运算的非空集合.我们称\(R\)是环,如果\((R,+)\)是阿贝尔群,\((R,\cdot)\)是幺半群,且\(R\)的乘法满足对加法的左右分配律.若将\((R,\cdot)\)是幺半群的条件修改为\((R,\cdot)\)是半群,我们称\(R\)是伪环.我们将在某些部分平行地构建出......