欢迎光临散文网 会员登陆 & 注册

R语言基础课程 6讲

2023-08-13 21:28 作者:像个陀螺转个不停  | 我要投稿

vector

matrix x=matrix(1:20,nrow=5,ncol=4,byrow=TRUE)

按行填充;

x=matrix(1:20,nrow=5,ncol=4,byrow=FALSE)

按列填充;

x[2,]第二行

x[,2]第二列

x[2,c(2,4)];

rnames=c("cat","dog","bird","pig")

cnames=c("apple","pencil")

x=matrix(1:8,nraw=4,ncol=2,byraw=TRUE)

colnames(x)=cnames

rawnames(x)=rnames;

Array(多维矩阵)

dim1=c("A1","A2")

dim2=c("B1","B2","B3")

dim3=c("C1","C2","C3","C4")

dim4=c("D1","D2","D3")

z=array(1:72,c(2,3,4,3),dimname=list(dim1,dim2,dim3,dim4))

z[1,2,3,];

data frame(大矩阵)

patientID=c(1,2,3,4)

age=c(25,34,28,52)

status=c("poor","improved","excellent","poor")

patientdata=data.frame(patientID,age,status)

patientdata[1:3](列)

patientdata[c(1,3),1:3];

attach(引入) and detach(删除)

list(最复杂的数据集,不限维度和数据长度)

mylist=list(avim,x)(包含两个数据集)

mylist[[2]][2]第二个数据集的第二个数

mylist[[2]][1:2]第二个数据集按列数的前两个数

mylist[[2]][1:2,3]第二个数据集前两行的第三个数


graphs

par(mfrow=c(2,2))(2×2的网格同时画多个图)

pch(图标)、cex(大小)、lty(线型)、lwd(宽度)

plot(rnorm<20>,type="l",lty=5)

text,axes,and legends

title(main="normal dist")

layout:

attach(mtcars)

layout(matrix(c(1,1,2,3),2,2,byrow=TRUE))

hist(wt)第一个图1,1,占两个坑

hist(mpg)第二个图2,占一个坑

hist(disp)第三个图3,占一个坑

detach(mtcars);

R语法

opertors

==,!=(不等于),x|y, x&y(同时满足)

(three valued logic)

control flow

1.For loop(不断循环,直到变为false)

for(i in 1:10)

2.while loop

i=1

while(i<=10){print(i)

i=i+1}

避免这两种在实践中的应用,运算慢

if:

if(){}

else(){}

switch:

feelings=c("sad","afraid")

for(i in feelings){

print(

switch(i,

happy="I am glad",

afraid="There is nothing to fear",

sad="Cheer up"

)

)

}

User-defined function:

myfunction=function(x,a,b,c){

return(a*sin(x)^2-b*x+c)}

curve(myfunction(x,20,3,4),xlim=c(1,20))

作图技巧,基本统计学分析方法

library(vcd)——用他的数据

创建一个名为counts的数据集

barplot(counts,main=“ ”,xlab=“ ”,ylab=“ “)

barplot(counts,main=“ ”,xlab=“ ”,ylab=“ “,horiz=TRUE)实现柱状图的倒置

main=stacked bar plot,grouped bar plot

Pie chart

paste实现字符串的拼接

\n:另起一行

explode:块与块之间的分离程度

Fan plot

Dot plot

summary:自动对变量进行分析

head:举例给看一下数据变量情况

table:

table(cut(数据集,seq(10,34,by=2)))10到34,以2递增

correlation:

cov:协方差

var:多组

cor:两两对应

T检验

t.test()

wilcox.test()

binom.test()

library(nortest)检验是否为正态分布

http://mathesaurus.sourceforge.net/octave-r.html 区分R和matlab指令的网址

矩阵的乘法

%*%

t(A):A的转置

colMeans:每列的平均数

colSums:每列的总和

rawMeans:每行的平均数

rawSums:每行的总和

crossprod:x %*% t(y)

crossprod(A):A %*% A

solve

diag(X):对角线

eigen(X):eigen values,eigen vectors

factor:标记

factor=factor(rep(c(1:3),times=5))

1到3,重复5次

x=sample(100,15)

0到100中取出15个数

tapply(x,factor,mean)

factor给X做标记,求x标记为1、2或3的数值的平均数

另外,利用print出点东西从而知道每个程序运行到了哪里

谷歌查询

英语学习

The R Project

The R journal

R Bloggers

Planet R

R Graph Gallery(作图技巧)

R Graphics Manual(作图技巧)

Journal of Statistical Software

Quick-R(包含R in Action的内容,假定读者已经有了很好的统计学基础)

书籍:

R Cookbook

R in Action(R语言实战)

ggplot2(数据可视化)

install.packages('ggplot2')

Advanced R





















R语言基础课程 6讲的评论 (共 条)

分享到微博请遵守国家法律