r语言中删除向量的某些元素

Vectors in R are the fundamental data types. This is because the R compiler treats all scalars (numerics, integers, etc.) and matrices as special cases of vectors.

R中的向量是基本数据类型。 这是因为R编译器将所有标量(数字,整数等)和矩阵都视为向量的特殊情况。

From a data scientist’s perspective, you can consider a vector as a collection of observations across an interval of time, such as temperatures read every day, total sales for the day, etc. R provides several relevant functions to handle vectors from this perspective.

从数据科学家的角度来看,您可以将向量视为一段时间内观察值的集合,例如每天读取的温度,一天的总销售额等。R从此角度提供了一些相关函数来处理向量。

在R中创建向量 (Creating Vectors in R)

The creation of a vector is done using the c() function.

向量的创建是使用c()函数完成的。


myvec <- c(3.1,45,1,2,80)

R language provides us the functionality to dynamically calculate values and assign them to vectors.

R语言为我们提供了动态计算值并将其分配给向量的功能。


> myvec2 <- c(3,5*8,(9/4))
> myvec2
[1]  3.00 40.00  2.25

We can create vectors using previously created variables.

我们可以使用先前创建的变量创建矢量。


> a <- 10
> b <-14.8
> c <-2
> myvec3 <- c(1,a,b,c)
> myvec3
[1]  1.0 10.0 14.8  2.0

We can also create a vector using two or more of the existing vectors.

我们还可以使用两个或多个现有向量创建向量。


> bigvec <-c(myvec,myvec2)
> bigvec
[1]  3.10 45.00  1.00  2.00 80.00  3.00 40.00  2.25

Vectors can have any number of items of the same data type (also sometimes known as the mode). Note: We cannot mix data types when we’re creating vectors in R.

向量可以具有相同数据类型的任何数量的项目(有时也称为mode )。 注意:在R中创建向量时,不能混合数据类型。

R语言中向量的运算 (Operations on Vectors in R Language)

Vectors are stored contiguously in the memory, similar to C. You can index the elements in a vector, extract subsets of vectors, sort and perform routine mathematical operations over vectors element-wise. We shall look at some examples that make these clear.

向量与C相似,连续存储在内存中。您可以索引向量中的元素,提取向量的子集,对向量进行逐元素排序并执行常规数学运算。 我们将看一些使这些事情变得清楚的例子。

向量的索引元素 (Indexing elements of a vector)

The elements of a vector can be extracted by using their index in a manner similar to accessing array elements. The following code snippet provides you with an example.

可以通过使用其索引以类似于访问数组元素的方式提取向量的元素。 以下代码段为您提供了一个示例。


> bigvec
[1]  3.10 45.00  1.00  2.00 80.00  3.00 40.00  2.25
> bigvec[2]
[1] 45
> avar <- bigvec[7]
> avar
[1] 40

When you try accessing an element beyond the vector’s size, R returns an NA value.

当您尝试访问超出向量大小的元素时,R返回一个NA值。

用R语言获取向量的长度 (Getting the length of a vector in R language)

Oftentimes, we deal with data from a dataset we download off the internet. We read entire columns into vector variables and may not be aware of the dimensions beforehand. In these cases, the length will be an important parameter to know so that we don’t run into NA values when working with data. The length of a vector can be known using a length() function.

通常,我们处理从互联网下载的数据集中的数据。 我们将整列读入向量变量,并且可能事先不知道尺寸。 在这些情况下,长度将是一个重要的参数,因此在处理数据时我们不会遇到NA值。 使用length()函数可以知道向量的length()


> length(bigvec)
[1] 8

R中的子集向量 (Subsetting vectors in R)

When dealing with long vectors, it is sometimes necessary to extract only the elements of interest from the vector. We can do this by making use of subsetting in R.

处理长向量时,有时有时仅需要从向量中提取感兴趣的元素。 我们可以通过使用R中的子集来做到这一点。


> bigvec
[1]  3.10 45.00  1.00  2.00 80.00  3.00 40.00  2.25#Extract the last element of a vector - where index equals length
> bigvec[length(x=bigvec)]
[1] 2.25#Extract the last but one element - subtract one from the length
> bigvec[length(x=bigvec)-1]
[1] 40#Extract all elements except for the first element
> bigvec[-1]
[1] 45.00  1.00  2.00 80.00  3.00 40.00  2.25#All elements except for the second one
> bigvec[-2]
[1]  3.10  1.00  2.00 80.00  3.00 40.00  2.25#Elements from index 1 to index 3
> bigvec[1:3]
[1]  3.1 45.0  1.0#Extract elements at specified indiced 1 and 5
> bigvec[c(1,5)]
[1]  3.1 80.0

We will look deeper into subsetting when we are working with real datasets in our further tutorials.

在进一步的教程中,当我们使用真实数据集时,我们将更深入地研究子集。

产生序列 (Generating Sequences)

Sequences are vectors in R that are generated using a sequence operator (:). They can also be generated using the seq function. These two methods are illustrated below.

序列是使用一个序列生成的操作者中的R矢量( : )。 它们也可以使用seq函数生成。 这两种方法如下所示。


> 4:10
[1]  4  5  6  7  8  9 10#From represents the starting range and to represents the ending range
#By is the increment factor.
> seq(from=1,to=20,by=2)[1]  1  3  5  7  9 11 13 15 17 19#By value is negative for decreasing sequences
> seq(from=10,to=2,by=-1)
[1] 10  9  8  7  6  5  4  3  2

Instead of using a by parameter, you can also supply a length.out parameter to indicate the length you need and get evenly spaced values from the starting range to ending range.

除了使用by参数,您还可以提供length.out参数来指示所需的长度,并获得从开始范围到结束范围的均匀间隔的值。


> seq(from=3,to=20,length.out=25)[1]  3.000000  3.708333  4.416667  5.125000  5.833333  6.541667  7.250000  7.958333[9]  8.666667  9.375000 10.083333 10.791667 11.500000 12.208333 12.916667 13.625000
[17] 14.333333 15.041667 15.750000 16.458333 17.166667 17.875000 18.583333 19.291667
[25] 20.000000

Vectors can be repeated using the rep function in R. The usage of rep is illustrated below.

载体可以使用重复rep在R.的使用功能rep如下所示。


> rep(x=1,times=5)
[1] 1 1 1 1 1

The x can be replaced by a vector to obtain a repeating vector as follows.

可以将x替换为向量,从而获得重复向量,如下所示。


> rep(x=bigvec, times=3)[1]  3.10 45.00  1.00  2.00 80.00  3.00 40.00  2.25  3.10 45.00  1.00  2.00 80.00
[14]  3.00 40.00  2.25  3.10 45.00  1.00  2.00 80.00  3.00 40.00  2.25

排序向量 (Sorting vectors)

Vectors can be sorted in ascending or descending order using the sort() function in the following manner.

可以使用sort()函数按以下方式sort()向量进行升序或降序sort()


#Sorts in ascending order by default.
#decreasing=FALSE is an optional parameter.
> sort(bigvec, decreasing = FALSE)
[1]  1.00  2.00  2.25  3.00  3.10 40.00 45.00 80.00#Sort in descending order
> sort(bigvec, decreasing=TRUE)
[1] 80.00 45.00 40.00  3.10  3.00  2.25  2.00  1.00

向量算术 (Vector arithmetic)

Vector arithmetic has been covered in the operators in R discussion earlier. One important point about vector arithmetic is recycling. When the specified vector operation has two vectors with mismatched length as operands, R simply recycles the values from the shorter vector until it reaches the length.

在较早的R讨论中,向量算术已包含在运算符中 。 向量算术的一个重要方面是循环 。 当指定的向量运算具有两个长度不匹配的向量作为操作数时,R会简单地循环利用较短向量的值,直到达到长度为止。


> a <-c(0,1)
> b <-c(1,2,3,4,5)#The new value of a after recycling will be (0,1,0,1,0) which gets added to b.
> a+b
[1] 1 3 3 5 5

翻译自: https://www.journaldev.com/35322/vectors-in-r

r语言中删除向量的某些元素

r语言中删除向量的某些元素_R中的向量相关推荐

  1. R语言dplyr包summarise_at函数计算dataframe数据中多个数据列(通过向量指定)的均值和中位数、指定na.rm参数配置删除缺失值

    R语言dplyr包summarise_at函数计算dataframe数据中多个数据列(通过向量指定)的均值和中位数.指定na.rm参数配置删除缺失值(Summarize with Custom Fun ...

  2. R语言dplyr包summarise_at函数计算dataframe数据中多个数据列(通过向量指定)的计数个数、均值和中位数、使用funs函数指定函数列表

    R语言dplyr包summarise_at函数计算dataframe数据中多个数据列(通过向量指定)的计数个数.均值和中位数.使用funs函数指定函数列表 目录

  3. R语言dplyr包summarise_at函数计算dataframe数据中多个数据列(通过向量指定)的计数个数、均值和中位数、使用list函数指定函数列表并指定自定义函数名称

    R语言dplyr包summarise_at函数计算dataframe数据中多个数据列(通过向量指定)的计数个数.均值和中位数.使用list函数指定函数列表并指定自定义函数名称 目录

  4. R语言dplyr包summarise_at函数计算dataframe数据中多个数据列(通过向量指定)的方差

    R语言dplyr包summarise_at函数计算dataframe数据中多个数据列(通过向量指定)的方差 目录 R语言dplyr包summarise_at函数

  5. c语言中去掉最小值,2020-07-12(C语言)从顺序表中删除具有最小值的元素(假设唯一)并由函数返回被删除元素的值。空出的位置由最后一个元素填补,若顺序表为空则显示出错信息并退出运行。...

    //从顺序表中删除具有最小值的元素(假设唯一)并由函数返回被删除元素的值.空出的位置由最后一个元素填补,若顺序表为空则显示出错信息并退出运行. include include define MAXSI ...

  6. R语言贝叶斯方法在生态环境领域中的高阶技术

    贝叶斯统计学即贝叶斯学派是一门基本思想与传统基于频率思想的统计学即频率学派完全不同的统计学方法,它在统计建模中具有灵活性和先进性特点,使其可以轻松应对复杂数据和模型结构. 然而,很多初学者在面对思想. ...

  7. R语言使用basename函数获取数据链接地址中的文件名称(removes all of the path up to and including the last path separator )

    R语言使用basename函数获取数据链接地址中的文件名称(removes all of the path up to and including the last path separator (i ...

  8. R语言data.table导入数据实战:data.table中编写函数并使用SD数据对象

    R语言data.table导入数据实战:data.table中编写函数并使用SD数据对象 目录 R语言data.table导入数据实战:data.table中编写函数并使用SD数据对象 #data.t ...

  9. R语言ggplot2可视化:通过在element_text函数中设置ifelse判断条件自定义标签文本的显示格式:例如、粗体、斜体等

    R语言ggplot2可视化:通过在element_text函数中设置ifelse判断条件自定义标签文本的显示格式:例如.粗体.斜体等 目录

最新文章

  1. django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3
  2. [转载] 使用backbone.js、zepto.js和trigger.io开发HTML5 App
  3. 交叉熵(cross_entropy)作为损失函数在神经网络中的作用
  4. html图片分类插件,Quicksand-jQuery超酷图片分类插件
  5. 编程语言排行分析,从2009到2019。
  6. Linux怎样创建FTP服务器--修改用户默认目录-完美解决 - 费元星
  7. 盘阵多路径学习(转)
  8. 剑指 Offer JZ35 复杂链表的复制
  9. Android 联系人信息的获取
  10. IIC原理超详细讲解---值得一看
  11. mac安装仿宋GB2312字体
  12. 提高情商,从这几方面做
  13. Spring MVC 详细信息讲解资料
  14. I love you
  15. Error:Module “./antd/es/badge/style“ does not exist in container. while loading “./antd/es/badge/sty
  16. 大学生集体户口面临孩子落户困境
  17. 高德地图进行省市下钻(vue)
  18. 掌财社:千亿市值“插座茅”被反垄断调查!高瓴、社保基金踩雷 公司火线回应
  19. python语言合法命名是_Python命名规范
  20. 教你如何收拾发短信的骗子们

热门文章

  1. ios 将随意对象存进数据库
  2. IE10-浏览器实现placeholder效果
  3. C#中自己动手创建一个Web Server(非Socket实现)
  4. 设置窗体的可见性无效
  5. 自己写的一个执行带参数的sql,PreparedStatement
  6. Python标准库05 存储对象 (pickle包,cPickle包)
  7. 后退与hash的问题
  8. Delphi学习之函数 ⑨汉字拼音功能函数
  9. 【原创翻译】深入理解javascript事件处理函数绑定三部曲(一)——早期的事件处理函数...
  10. [转载] JAVA出现空指针异常(初学者)