R语言学习(2)

向量矩阵和数组

1.vector函数可以创建指定类型、长度的矢量

(其结果中的值可以是0,FLASE,空字符串)

> vector("numeric",5)

[1] 0 0 0 0 0

> vector("complex",6)

[1] 0+0i 0+0i 0+0i 0+0i 0+0i 0+0i

> vector("logical",6)

[1] FALSE FALSE FALSE FALSE FALSE FALSE

> vector("character",4)

[1] "" "" "" ""

> vector("list",5)

[[1]]

NULL

[[2]]

NULL

[[3]]

NULL

[[4]]

NULL

[[5]]

NULL

使用每个类型的包装函数创建矢量与vector等价

> numeric(5)

[1] 0 0 0 0 0

> complex(5)

[1] 0+0i 0+0i 0+0i 0+0i 0+0i

2.序列

使用seq.int函数创建序列

> seq.int(5,8)     #相当于 5:8

[1] 5 6 7 8

> seq.int(3,12,2)     
 #可指定步长为2

[1]  3  5  7  9 11

seq_len函数创建一个从1到它的输入值的序列

> seq_len(5)

[1] 1 2 3 4 5

3.长度

length函数查向量长度

> length(4:9)

[1] 6

nchar函数查字符串长度

> str <- c("Peter","Kate","Jim")

> length(str)

[1] 3

> nchar(str)

[1] 5 4 3

4.索引

> x <- (1:5)^2

> x[c(1,3,5)]

[1]  1  9 25

> x[c(-2,-4)]

[1]  1  9 25

> x[c(TRUE,FALSE,TRUE,FALSE,TRUE)]

[1]  1  9 25

5.数组

使用array函数创建数组,为他们传入两个向量(值和维度)作为参数

> (three_d_array <- array(1:24,dim = c(4,3,2),dimnames = list(c("one","two","three","four"),c("five","six","seven"),c("eight","nine"))))

, , eight

five six seven

one      1   5     9

two      2   6    10

three    3   7    11

four     4   8    12

, , nine

five six seven

one     13  17    21

two     14  18    22

three   15  19    23

four    16  20    24

6.矩阵

创建矩阵使用matrix函数只需要指定行数和列数

> (a_matrix <- matrix(1:12,nrow = 4,dimnames = list(c("one","two","three","four"),c("one","two","three"))))

one two three

one     1   5     9

two     2   6    10

three   3   7    11

four    4   8    12

创建矩阵时传入的值默认会按列填充,可指定参数byrow = TRUE来按行填充

> (a_matrix <- matrix(1:12,nrow = 4,byrow = TRUE,dimnames = list(c("one","two","three","four"),c("one","two","three"))))

one two three

one     1   2     3

two     4   5     6

three   7   8     9

four   10  11    12

7.行名 rownames

列名 colnames

维度名 dimnames

8.索引数组

> a_matrix[1,c("two","three")]

two three

2     3

> a_matrix[1, ]

one   two three

1     2     3

> a_matrix[ ,c("one","three")]

one three

one     1     3

two     4     6

three   7     9

four   10    12

9.合并矩阵

c函数可以把矩阵转化成向量然后合并

> (another_matrix <- matrix(seq.int(2,24,2),nrow = 4,dimnames = list(c("one","two","three","four"),c("one","two","three"))))

one two three

one     2  10    18

two     4  12    20

three   6  14    22

four    8  16    24

> c(a_matrix,another_matrix)

[1]  1  4  7 10  2  5  8 11  3  6  9 12  2  4  6  8 10 12 14 16 18 20 22 24

使用cbind、rbind函数可以按行、列绑定矩阵

> cbind(a_matrix, another_matrix)

one two three one two three

one     1   2     3   2  10    18

two     4   5     6   4  12    20

three   7   8     9   6  14    22

four   10  11    12   8  16    24

> rbind(a_matrix, another_matrix)

one two three

one     1   2     3

two     4   5     6

three   7   8     9

four   10  11    12

one     2  10    18

two     4  12    20

three   6  14    22

four    8  16    24

10.数组算数

矩阵内乘:

> a_matrix %*% t(a_matrix)

one two three four

one    14  32    50   68

two    32  77   122  167

three  50 122   194  266

four   68 167   266  365

矩阵外乘:

> 1:3 %o% 4:6

[,1] [,2] [,3]

[1,]    4    5    6

[2,]    8   10   12

[3,]   12   15   18

R语言学习(2)

时间: 2024-08-07 08:39:37

R语言学习(2)的相关文章

R语言学习笔记2——绘图

R语言提供了非常强大的图形绘制功能.下面来看一个例子: > dose <- c(20, 30, 40, 45, 60)> drugA <- c(16, 20, 27, 40, 60)> drugB <- c(15, 18, 25, 31, 40) > plot(dose, drugA, type="b") > plot(dose, drugB, type="b") 该例中,我们引入了R语言中第一个绘图函数plot.pl

R语言学习笔记

參考:W.N. Venables, D.M. Smith and the R DCT: Introduction to R -- Notes on R: A Programming Environment for Data Analysis and Graphics,2003. http://bayes.math.montana.edu/Rweb/Rnotes/R.html 前言:关于R 在R的官方教程里是这么给R下注解的:一个数据分析和图形显示的程序设计环境(A system for data

R语言学习(5)-字符串和因子

字符串和因子 1.字符串 创建字符串 > c("HELLO","WORLD") [1] "HELLO" "WORLD" 使用paste函数连接字符串 > paste(c("hello","hi"),"world") [1] "hello world" "hi world" > paste(c("hel

R语言学习中的小bug:R中矩阵相乘错误于A %*% B: 需要数值/复数矩阵/矢量参数

遇到了小bug: R中矩阵相乘错误于A %*% B: 需要数值/复数矩阵/矢量参数 看到网上别人的做法,发现了用class(A)和class(B)之后才发现,是因为读入的时候数据的类型不对,A.B的类型并不是matrix,才导致了这个问题. 用as.matrix来变型一下,就OK了. R语言学习中的小bug:R中矩阵相乘错误于A %*% B: 需要数值/复数矩阵/矢量参数,布布扣,bubuko.com

R语言学习(3)

列表和数据框 1.列表 list函数创建列表 > (a_list <- list(c(1,1,2,5,14,42),month.abb,matrix(c(3,-8,1,-3),nrow=2),asin)) [[1]] [1]  1  1  2  5 14 42 [[2]] [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul&qu

R语言学习(4)-环境和函数

环境和函数 1.环境 使用new.env函数创建环境 > an_environment <- new.env() 向环境中分配变量与列表相同 > an_environment[["pythag"]] <- c(12,15,20,21) > an_environment$root <- polyroot(c(6,-5,1)) > assign("moonday",weekdays(as.Date("1969/07/2

R语言学习(1)

将R作为科学计算器使用 1.例: > 1:5 + 6:10 [1]  7  9 11 13 15 > c(1,3,5,7,9)+c(2,4,6,8,10) [1]  3  7 11 15 19 > median(2:5) [1] 3.5 > 1:10 / 3 [1] 0.3333333 0.6666667 1.0000000 1.3333333 1.6666667 2.0000000 2.3333333 [8] 2.6666667 3.0000000 3.3333333 > 

R语言学习笔记 之 可视化地研究参议员相似性

基于相似性聚类 很多时候,我们想了解一群人中的一个成员与其他成员之间有多么相似.例如,假设我们是一家品牌营销公司,刚刚完成了一份挂怒有潜力新品牌的研究调查问卷.在这份调查问卷中,我们向一群人展示了新品牌的几个特征,并且要求他们对这个新品牌的每个特征按五分制打分.同时也收集了目标人群的社会经济特征,例如:年龄.性别.种族.住址的邮编以及大概的年收入. 通过这份调查问卷,我们想搞清楚品牌如何吸引不同社会经济特征的人群.最重要的是,我们想要知道这个品牌是否有很大的吸引力.换个角度想这个问题,我们想看看

R语言学习笔记(二)

今天主要学习了两个统计学的基本概念:峰度和偏度,并且用R语言来描述. > vars<-c("mpg","hp","wt") > head(mtcars[vars]) mpg hp wt Mazda RX4 21.0 110 2.620 Mazda RX4 Wag 21.0 110 2.875 Datsun 710 22.8 93 2.320 Hornet 4 Drive 21.4 110 3.215 Hornet Sportab