使用冒号运算带有数值数据(数值的增加为1)

# Creating a sequence from 5 to 13.
v <- 5:13
print(v) # Creating a sequence from 6.6 to 12.6.
v <- 6.6:12.6
print(v) # If the final element specified does not belong to the sequence then it is discarded.
v <- 3.8:11.4
print(v)

当我们上面的代码执行时,它产生以下结果:

[1]  5  6  7  8  9 10 11 12 13
[1] 6.6 7.6 8.6 9.6 10.6 11.6 12.6
[1] 3.8 4.8 5.8 6.8 7.8 8.8 9.8 10.8

使用序列 (Seq.) 运算符

# Create vector with elements from 5 to 9 incrementing by 0.4.
print(seq(5, 9, by=0.4))

当我们上面的代码执行时,它产生以下结果:

 [1] 5.0 5.4 5.8 6.2 6.6 7.0 7.4 7.8 8.2 8.6 9.0

charToRaw:把字符串转化为数字数组进行输出。

使用 c() 函数

非字符值强制转换为字符类型,如果该元素之一是字符。

# The logical and numeric values are converted to characters.
s <- c('apple','red',5,TRUE)
print(s)

当我们上面的代码执行时,它产生以下结果:

[1] "apple" "red"   "5"     "TRUE" 

访问向量元素

一个向量的元素使用索引访问。[]括号是用来进行索引。索引位置始于1。 给出索引负值将从结果元素中丢弃。TRUE, FALSE 或 0 和 1。也可用于索引。

# Accessing vector elements using position.
t <- c("Sun","Mon","Tue","Wed","Thurs","Fri","Sat")
u <- t[c(2,3,6)]//访问2,3,6位置的元素
print(u) # Accessing vector elements using logical indexing.
v <- t[c(TRUE,FALSE,FALSE,FALSE,FALSE,TRUE,FALSE)]//凡是false的位置,结果不做输出
print(v) # Accessing vector elements using negative indexing.
x <- t[c(-2,-5)]//2和5索引位置数据由于是负数,表示不输出
print(x) # Accessing vector elements using 0/1 indexing.
y <- t[c(0,0,0,0,0,0,1)] //如果是数组相等的位置,0位置表示数值不做输出。
print(y)

当我们上面的代码执行时,它产生以下结果:

[1] "Mon" "Tue" "Fri"
[1] "Sun" "Fri"
[1] "Sun" "Tue" "Wed" "Fri" "Sat"
[1] "Sun"

向量操作

向量运算

相同长度的两个矢量可以加,减,乘或除给出的结果为向量输出。

# Create two vectors.
v1 <- c(3,8,4,5,0,11)
v2 <- c(4,11,0,8,1,2) # Vector addition.
add.result <- v1+v2
print(add.result) # Vector substraction.
sub.result <- v1-v2
print(sub.result) # Vector multiplication.
multi.result <- v1*v2
print(multi.result) # Vector division.
divi.result <- v1/v2
print(divi.result)

当我们上面的代码执行时,它产生以下结果:

[1]  7 19  4 13  1 13
[1] -1 -3 4 -3 -1 9
[1] 12 88 0 40 0 22
[1] 0.7500000 0.7272727 Inf 0.6250000 0.0000000 5.5000000

向量元素回收利用

如果我们算术运算不等长的两个向量,那么短的向量的元素被循环以完成操作。

v1 <- c(3,8,4,5,0,11)
v2 <- c(4,11)
# V2 becomes c(4,11,4,11,4,11) add.result <- v1+v2
print(add.result) sub.result <- v1-v2
print(sub.result)

当我们上面的代码执行时,它产生以下结果:

[1]  7 19  8 16  4 22
[1] -1 -3 0 -6 -4 0

向量元素排序

一个向量中元素可以使用 sort()函数进行排序。

v <- c(3,8,4,5,0,11, -9, 304)

# Sort the elements of the vector.
sort.result <- sort(v)
print(sort.result) # Sort the elements in the reverse order.
revsort.result <- sort(v, decreasing = TRUE)
print(revsort.result) # Sorting character vectors.
v <- c("Red","Blue","yellow","violet")//按照首字母进行排序
sort.result <- sort(v)
print(sort.result) # Sorting character vectors in reverse order.
revsort.result <- sort(v, decreasing = TRUE)
print(revsort.result)

当我们上面的代码执行时,它产生以下结果:

[1]  -9   0   3   4   5   8  11 304
[1] 304 11 8 5 4 3 0 -9
[1] "Blue" "Red" "violet" "yellow"
[1] "yellow" "violet" "Red" "Blue"

R语言多元素向量的更多相关文章

  1. R语言入门:向量的运算

    向量之间的加减乘除运算: > x <- 1 > x [1] 1 2 3 4 5 6 7 8 9 10 > x=x+1 > x [1] 2 3 4 5 6 7 8 9 10 ...

  2. R语言入门:向量索引

    这节的内容是建立在之前我们对R语言最基本向量赋值的基础之上的,笔者本人学完R当中向量的索引感觉异常舒适,因为这个比Python的索引爽多了,是什么值开始索引就从哪里开始索引,到哪里结束就在哪里结束,而 ...

  3. R语言入门:向量初探

    R语言主要用于统计,因此引入了向量这个概念将更好地进行统计计算,在其他无法引入向量的语言当中则会使用循环来计算一些大规模的数据,在R语言当中则不需要,下面我们来看看R语言当中向量的具体用法吧! 首先, ...

  4. R语言向量

    R语言基础:向量  心无咎 2012-04-02 13:37:00 向量(vector)1.seq():产生有规律的数列,间距省略时默认值为1.        例1:seq(10, 20, 0.5)  ...

  5. 数据攻略●R语言自述

    (注明:以下文章均在Linux操作系统下执行) 一.R语言简介 R语言是用于统计分析,图形表示和报告的编程语言和软件环境.R语言由Ross Ihaka和Robert Gentleman在新西兰奥克兰大 ...

  6. R语言数据结构二

    上节我们讲到R语言中的基本数据类型,包括数值型,复数型,字符型,逻辑型以及对应的操作和不同数值类型之间的转换.众所周知,R语言的优势在于进行数据挖掘,大数据处理等方面,因此单个的数据并不能满足我们的需 ...

  7. 中部:执具 | R语言数据分析(北京邮电大学)自整理笔记

    第5章工欲善其事.必先利其器 代码,是延伸我们思想最好的工具. 第6章基础编程--用别人的包和函数讲述自己的故事 6.1编程环境 1.R语言的三段论 大前提:计算机语言程序=算法+数据结构 小前提:R ...

  8. R语言基础:数组&列表&向量&矩阵&因子&数据框

    R语言基础:数组和列表 数组(array) 一维数据是向量,二维数据是矩阵,数组是向量和矩阵的直接推广,是由三维或三维以上的数据构成的. 数组函数是array(),语法是:array(dadta, d ...

  9. R语言学习笔记:向量

    向量是R语言最基本的数据类型. 单个数值(标量)其实没有单独的数据类型,它只不过是只有一个元素的向量. x <- c(1, 2, 4, 9) x <- c(x[1:3], 88, x[4] ...

随机推荐

  1. apache mpm的一些问题

    win2003系统下apache环境,mpm_winnt.c模式,优化参数: ThreadsPerChild 说明:每个子进程建立的线程数,默认值:64,最大值:1920.网上查询资料建议设置在100 ...

  2. Linux 批量管理工具

    pssh/pscp(Python) ansible(Python) saltstack(Python) chef puppet(Ruby) fabric(Python)

  3. 关于类属性值校验的一点记录 【知识点Attribute】

    好久没有进来了,之前励志坚持写博客,记录自己在做代码搬运工这段历程中点滴,可是仅仅只坚持了几天,就放弃了!果然是,世上无难事,只要肯放弃!哈哈……闲话不多说,开始进入正题,给自己留点笔记,避免将来老了 ...

  4. how to trace the error log

    Executed as user: WTC\Ebw.Admin. Transaction (Process ID 95) was deadlocked on lock resources with a ...

  5. 【OCP 12c】最新CUUG OCP-071考试题库(62题)

    62.(13-17)choose the best answer: You need to list the employees in DEPARTMENT_ID 30 in a single row ...

  6. logstash同步mongodb数据到elasticsearch

    一.安装logstash 二.安装mongodb插件 cd D:\Software\ELK5.5.0\logstash-5.5.0\bin logstash-plugin install logsta ...

  7. html中文字溢出处理(text-overflow)

    文字溢出处理有两种方式: 一.css overflow:hidden;            white-space: nowrap;            text-overflow: ellips ...

  8. [转] 检查更新时出错:无法启动更新检查(错误代码为 4: 0x80070005 — system level)

    Google浏览器Chrome更新到时候提示错误:检查更新时出错:无法启动更新检查(错误代码为 4: 0x80070005 -- system level),很有可能是Chrome更新服务被禁用了,我 ...

  9. 利用python 学习数据分析 (学习一)

    内容学习自: Python for Data Analysis, 2nd Edition         就是这本 纯英文学的很累,对不对取决于百度翻译了 前情提要: 各种方法贴: https://w ...

  10. ubuntu 14.04网卡配置以及关闭防火墙

    一.Ubuntu网卡配置如下: 在文件/etc/network/interfaces中进行以下配置 auto lo iface lo inet lookback auto eth0 iface eth ...