最新R软件期末考试复习提纲

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#期末考试专项复习

#一、矩阵与数据框

#1.生成特定的矩阵与数据框

#矩阵

#方法一

a=array(1:10,dim=c(2,5))

rownames(a)=1:2

colnames(a)=c("one","two","three","four","five")

a

dimnames(a)=list(1:2,c("one","two","three","four","five")) nrow=nrow(a)

ncol=ncol(a)

dim(a)

#方法二

a=matrix(1:10,nrow=2,byrow=F)

rownames(a)=1:2

colnames(a)=c("one","two","three","four","five")

a=matrix(1:10,nrow=2,byrow=F,

dimnames=list(1:2,c("one","two","three","four","five"))

)

#数据框的生成

df=data.frame(

Name=c("Alice","Becka","James","Jeffrey","John"),

Sex=c("F","F","M","M","M"),

Age=c(13,13,12,13,12),

Height=c(56.5,65.3,57.3,62.5,59.0),

Weight=c(84.0,98.0,83.0,84.0,99.5)

);df

Lst=list(Name=c("Alice","Becka","James","Jeffrey","John"), Sex=c("F","F","M","M","M"),

Age=c(13,13,12,13,12),

Height=c(56.5,65.3,57.3,62.5,59.0),

Weight=c(84.0,98.0,83.0,84.0,99.5))

Lst

Lst[["Name"]]

Lst["Name"]

Lst[1]

Lst[[1]]

Lst$Name

df=as.data.frame(Lst)

df

x=array(1:6,dim=c(2,3))

as.data.frame(x)

#数据框的引用

df[1:2,3:5]

df[["Height"]]

df$Weight

names(df)#此属性一定非空

rownames(df)=c("one","two","three","four","five") df

attach(df)

r=Height/Weight

r

df$r=r

names(df)

detach()

r=Height/Weight

#2.矩阵的运算

a=diag(1:3)

a[2][1]=1

a

#1转置运算

t(a)

#2行列式

det(a)

#3向量内积

x=1:5

y=2*1:5

x%*%y

t(x)%*%y

crossprod(x,y)

#4向量的外积

x%*%t(y)

tcrossprod(x,y)

outer(x,y)

x%o%y

#矩阵的乘法

a=array(1:9,dim=c(3,3))

b=array(9:1,dim=c(3,3))

x=1:3

a*b

a%*%b

x%*%a%*%x

crossprod(a,b)#t(a)%*%b

tcrossprod(a,b)#a%*%t(b)

#矩阵的逆

solve(a)

b=1:3

solve(a,b)#ax=b的解

#矩阵的特征值与特征向量

sm=eigen(a)

sm

e=diag(1:3)

svde=svd(e)

svde

attach(svde)

u%*%diag(d)%*%t(v)

#与矩阵运算有关的函数

#取维数

a=diag(1:4)

nrow(a)

ncol(a)

#矩阵的合并

x1=rbind(c(1,2),c(3,4))

x2=x1+10

x3=cbind(x1,x2)

x3

x4=rbind(x1,x2)

x4

cbind(1,x1)

#矩阵的拉直

a=matrix(1:6,ncol=2,

dimnames=list(c("one","two","three"),

c("first","second")),byrow=T)

as.vector(a)

#apply函数

apply(a,1,mean)

apply(a,2,sum)

tapply(1:5,factor(c("f","f","m","m","m")),mean) #第二题

#产生随机数

x=rnorm(100,0,1)

x

#画随机数的直方图

hist(x,freq=F)

#核密度曲线

density(x)

相关文档
最新文档