R语言统计入门第一章基础知识——1.2R语言基础

1.2 R语言基础

1.2.1 表达式和对象

和其他语言类似的理解表达式和对象

#1.2.2 函数和参数

args(plot.default)

args是一个函数,通过它可以调用查看其他函数的参数,以上例为调用查看了plot函数的参数,其实还可以通过?加上函数查看R的帮助文件。

function (x, y = NULL, type = “p”, xlim = NULL, ylim = NULL,
log = “”, main = NULL, sub = NULL, xlab = NULL, ylab = NULL,
ann = par(“ann”), axes = TRUE, frame.plot = axes, panel.first = NULL,
panel.last = NULL, asp = NA, …)
NULL

功能(变量x,变量y,图类,x坐标区间,y坐标区间,…)

c("Huey","Dewey","Louie")

[1] “Huey” “Dewey” “Louie”

c('Huey','Dewey','Louie')#字符向量,单引号或双引号亦可,左右保持一致

[1] “Huey” “Dewey” “Louie”

c(T,T,F,T)#逻辑向量

[1] TRUE TRUE FALSE TRUE

1.2.4 引用和转义序列


cat(c("Huey","Dewey","Louie"))#输出不带引号的字符串

Huey Dewey Louie

cat("Huey","Dewey","Louie","\n")#\转移字符\n换行
cat("What is\"R\"?\n")#\"可插入应用字符

What is"R"?
R中的转义字符和c++中的一个意思,都是将原本软件的编译字符转换为文本字符。

1.2.5 缺失值

1.2.6 生成向量的函数


c(42,57,12,39,1,3,4)

[1] 42 57 12 39 1 3 4

x<-c(1,2,3)
y<-c(10,20)
c(x,y,5)#连接向量

[1] 1 2 3 10 20 5

x<-c(red="Huey",blue="Dewey",green="Louie")#给元素命名
x

red blue green
“Huey” “Dewey” “Louie”

names(x)#提取名称

[1] “red” “blue” “green”

c(FALSE,3)

[1] 0 3

c(pi,"abc")

[1] “3.14159265358979” “abc”

c(FALSE,"abc")#连接不同类型的向量,会被转化为最少限制的类型

[1] “FALSE” “abc”

seq(4,9)#从4到9的整数

[1] 4 5 6 7 8 9

seq(4,10,2)#4到10序列间距为2

[1] 4 6 8 10

4:9#同等与seq(4,9)

[1] 4 5 6 7 8 9

oops<-c(7,9,13)
rep(oops,3)#参数为3的重复

[1] 7 9 13 7 9 13 7 9 13

rep(oops,1:3)#参数为向量1:3的重复

[1] 7 9 9 13 13 13

rep(1:2,c(10,15))#前10个观测值为1

[1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

rep(1:2,each=10)#等同于rep(1:2,c(10,10))

[1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2

1.2.7 矩阵和数组


x<-1:12
dim(x)<-c(3,4)#dim设置或更改维度,矩阵的储存以列为主
x

[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12

matrix(1:12,nrow = 3,byrow = T)#创建矩阵matrix,nrow为行数量,byrow=F为以列形式填充

[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12

x<-matrix(1:12,nrow = 3,byrow = T)
rownames(x)<-LETTERS[1:3]#对行名称变换,LETTERS大写字母
x

[,1] [,2] [,3] [,4]
A 1 2 3 4
B 5 6 7 8
C 9 10 11 12

t(x)#矩阵转置

A B C
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12

cbind(A=1:4,B=5:8,C=9:12)#按列将向量合并

A B C
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12

rbind(A=1:4,B=5:8,C=9:12)#按行将向量合并

[,1] [,2] [,3] [,4]
A 1 2 3 4
B 5 6 7 8
C 9 10 11 12

1.2.8 因子


pain<-c(0,3,2,2,1)
fpain<-factor(pain,levels=0:3)#factor创建因子,levels=0:3使用0~3这3个值
levels(fpain)<-c("none","mild","medium","severe")#将水平名称转换为特定的4个字符串
fpain

[1] none severe medium medium mild
Levels: none mild medium severe

as.numeric(fpain)#as.numeric()提取数字编码为1~4

[1] 1 4 3 3 2

levels(fpain)#levels提取水平因子的名称,一个因子的内置表达方式始终是从1开始的数字

[1] “none” “mild” “medium” “severe”

1.2.9 列表


intake.pre<-c(5260,5470,5640,6180,6390,6515,6805,7515,7515,8230,8770)
intake.post<-c(3910,4220,3885,5160,5645,4680,5265,5975,6790,6900,7335)
mylist<-list(before=intake.pre,after=intake.post)#list创建一个列表 ,列表各部分通过list中使用的参数来命名
mylist

$before
[1] 5260 5470 5640 6180 6390 6515 6805 7515 7515 8230 8770

$after
[1] 3910 4220 3885 5160 5645 4680 5265 5975 6790 6900 7335

mylist$before#list命名部分的提取

[1] 5260 5470 5640 6180 6390 6515 6805 7515 7515 8230 8770

1.2.10 数据框


d<-data.frame(intake.pre,intake.post)#创建数据框
d

intake.pre intake.post
1 5260 3910
2 5470 4220
3 5640 3885
4 6180 5160
5 6390 5645
6 6515 4680
7 6805 5265
8 7515 5975
9 7515 6790
10 8230 6900
11 8770 7335

d$intake.pre

[1] 5260 5470 5640 6180 6390 6515 6805 7515 7515 8230 8770

1.2.11 索引


intake.pre[5]#索引向量中一个具体的元素,此为第五个

[1] 6390

intake.pre[5]<-6390#修改该位置上元素的值
intake.pre[c(3,5,7)]#索引357序号的元素

[1] 5640 6390 6805

v<-c(3,5,7)
intake.pre[v]

[1] 5640 6390 6805

intake.pre[1:5]#索引前五个的值

[1] 5260 5470 5640 6180 6390

intake.pre[-c(3,5,7)]#负索引,得到除去357位置之外的其他元素

[1] 5260 5470 6180 6515 7515 7515 8230 8770

1.2.12 条件选择


intake.post[intake.pre>7000]#筛选了pre大于7000的post的值

[1] 5975 6790 6900 7335

intake.post[intake.pre>7000&intake.pre<=8000]#索引了pre7000到8000的post的值,|为或

[1] 5975 6790

intake.pre>7000&intake.pre<=8000#用逻辑向量进行索引的结果是逻辑值

[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE

is.na(x)#寻找x中那些被记录成缺失值的元素,输出结果是逻辑值

[,1] [,2] [,3] [,4]
A FALSE FALSE FALSE FALSE
B FALSE FALSE FALSE FALSE
C FALSE FALSE FALSE FALSE

1.2.13 数据框的索引


d<-data.frame(intake.pre,intake.post)
d[5,1]#给出第5行,第1列的数据

[1] 6390

d[5,]#给出第五行所有数据

intake.pre intake.post
5 6390 5645

d[d$intake.pre>7000,]#pre超过7000的所有数据

intake.pre intake.post
8 7515 5975
9 7515 6790
10 8230 6900
11 8770 7335

#数据框索引的分解
sel<-d$intake.pre>7000#将逻辑向量赋给sel
sel

[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE

d[sel,]#索引sel逻辑值为TRUE的数据框中的数据

intake.pre intake.post
8 7515 5975
9 7515 6790
10 8230 6900
11 8770 7335

d[1:2,]#索引前两行

intake.pre intake.post
1 5260 3910
2 5470 4220

head(d)#显示前六行

intake.pre intake.post
1 5260 3910
2 5470 4220
3 5640 3885
4 6180 5160
5 6390 5645
6 6515 4680

tail(d)#显示最后面六行

intake.pre intake.post
6 6515 4680
7 6805 5265
8 7515 5975
9 7515 6790
10 8230 6900
11 8770 7335

1.2.14 分组数据和数据框


energy#ISrW内置数据框

expend stature
1 9.21 obese
2 7.53 lean
3 7.48 lean
4 8.08 lean
5 8.09 lean
6 10.15 lean
7 8.40 lean
8 10.88 lean
9 6.13 lean
10 7.90 lean
11 11.51 obese
12 12.79 obese
13 7.05 lean
14 11.85 obese
15 9.97 obese
16 7.48 lean
17 8.79 obese
18 9.69 obese
19 9.68 obese
20 7.58 lean
21 9.19 obese
22 8.11 lean

exp.lean<-energy$expend[energy$stature=="lean"]#将能量数据表中身材为lean的expend数据赋给exp.lean
exp.obese<-energy$expend[energy$stature=="obese"]#类比上条
l<-split(energy$expend,energy$stature)#使用split根据分组生成一系列向量,将expend数据按stature分类后分离
l

$lean
[1] 7.53 7.48 8.08 8.09 10.15 8.40 10.88 6.13 7.90 7.05 7.48
[12] 7.58 8.11

$obese
[1] 9.21 11.51 12.79 11.85 9.97 8.79 9.69 9.68 9.19

1.2.15 隐式循环


lapply(thuesen,mean,na.rm=T)

$blood.glucose
[1] 10.3

$short.velocity
[1] 1.325652

sapply(thuesen,mean,na.rm=T)

blood.glucose short.velocity
10.300000 1.325652

replicate(10,mean(rexp(20)))#10次重复计算均值

[1] 0.6140350 0.8507473 1.2353548 1.3126414 1.0893192 0.7466570 0.8615039
[8] 1.0832498 0.9113315 0.5823115

m<-matrix(rnorm(12),4)
m

[,1] [,2] [,3]
[1,] -0.74121646 0.4237762 -0.3976827
[2,] 0.26132750 -0.5900554 -0.7094085
[3,] 0.02058380 -0.6935721 1.1334965
[4,] -0.08295565 -0.8083556 2.9608444

apply(m,1,max)#对m逐行取最大值

[1] 0.4237762 0.2613275 1.1334965 2.9608444

apply(m,2,min)#对m逐列取最小值

[1] -0.7412165 -0.8083556 -0.7094085

tapply(energy$expend,energy$stature,median)#该表由函数关于第二个参数定义的子组上的返回值构成,参数为为一列因子时生成一个交叉分类表

lean obese
7.90 9.69

1.2.16 排序


intake$post#内置数据集intake

[1] 3910 4220 3885 5160 5645 4680 5265 5975 6790 6900 7335

sort(intake$post)#sort对intake$post进行排序

[1] 3885 3910 4220 4680 5160 5265 5645 5975 6790 6900 7335

order(intake$post)#通过将其值依次置于3126等对intake$post排序

[1] 3 1 2 6 4 7 5 8 9 10 11

o<-order(intake$post)
n<-order(-intake$post)#逆序排序
intake$post[o]#等于sort(intake$post)

[1] 3885 3910 4220 4680 5160 5265 5645 5975 6790 6900 7335

intake$pre[o]#将pre按post从小到大的顺序排序

[1] 5640 5260 5470 6515 6180 6805 6390 7515 7515 8230 8770

intake$pre[n]#将pre按post从大到小的顺序排序

[1] 8770 8230 7515 7515 6390 6805 6180 6515 5470 5260 5640

intake.sorted<-intake[o,]#对整个数据框排序

猜你喜欢

转载自blog.csdn.net/qq_38742877/article/details/86481240
今日推荐