数据分析和R语言一

数据分析知识点

集中趋势指标

  均值mean  中位数median  众数mode

变异性指标

  极差range 方差variance 标准差standard deviation

  偏度skewness 峰度kurtosis

常见报表

  饼图 垂直柱 水平柱 线图 水泡

R语言

常用函数

创建向量和矩阵

  函数c( ), length( ), mode( ) 返回数据类型, rbind( ) 行绑定合并, cbind( )列绑定合并 

求平均值,和,连乘,最值,方差,标准差

  函数mean( ) 均值, sum( ), min( ), max( ), var( ) 方差, sd( ) 标准差, prod( ) 各项乘积寻求帮助 

  help()

 

seq( )按顺序产生数字   seq(5,20)   seq(5,121,by=2)  by 差值  

Which()   which.max()  which.min()    返回下标

rev( ) 逆转,sort( )排序  letters() 产生26个字母

matrix()  生成矩阵  matrix(c(1:12),nrow=3,ncol=4)

diag() 生成对角矩阵

rnorm() 随机产生正态数据   matrix(rnorm(16),4,4)

solve() 求逆

eigen() 求特征值和特征向量

data.frame() 数据框

plot() 画散点图

setwd() 设置工作目录     getwd()

read.table("abc.txt")   read.table("clipboard",header=F)  读剪切板  F 为False

library()加载包  install.package() 安装包

 

循环语句

for     for(i in 1:20) {a[i] = i*2 +3}

while    while(a[i]<100) {i=i+1;a[i]=a[i-1]+2}

R脚本

source()   执行R脚本   source("hello.r")

 

分布函数

正态分布函数rnorm( )
泊松分布函数rpois( )
指数分布函数rexp( )
Gamma分布函数rgamma( )
均匀分布函数runif( )
二项分布函数rbinom( )
几何分布函数rgeom( )

 

 代码示例

num = seq(10378001,10378100)
x1 = round(runif(100,min=80,max=100))
x2 = round(rnorm(100,mean=80,sd=7))
x3 = round(rnorm(100,mean=83,sd=18))
x3[which(x3>100)] =100
x = data.frame(num,x1,x2,x3)
write.table(x,file="mark.txt",row.names = F,col.names = F,sep = " ")
colMeans(x)
apply(x,2,mean)
apply(x,2,max)
apply(x,2,min)
apply(x[c("x1","x2","x3")],1,sum)
which.max(apply(x[c("x1","x2","x3")],1,sum))
x$num[which.max(apply(x[c("x1","x2","x3")],1,sum))]
hist(x$x1)
plot(x1,x2)
table(x$x1)
barplot(table(x$x1))
pie(table(x$x1))
# 箱尾图
boxplot(x$x1,x$x2,x$x3)
boxplot(x[2:4],col=c("red","green","blue"),notch = T)
boxplot(x$x1,x$x2,x$x3,horizontal = T)
# 星向图
stars(x[c("x1","x2","x3")])
stars(x[c("x1","x2","x3")],full=T,draw.segments = T)
stars(x[c("x1","x2","x3")],full=F,draw.segments = T)

#脸谱图
# library(aplpack)
# faces(x[c("x1","x2","x3")])

#茎叶图
stem(x$x1)

# QQ图
# 可用于判断是否正态分布
# 直线的斜率是标准差,截距是均值
# 点的散布越接近直线,则越接近正态分布
qqnorm(x1)
qqline(x1)

Sys.setlocale(, "zh_CN.UTF-8")
plot(x$x1,x$x2, main="数学分析与线性代数成绩的关系", xlab="数学分析",
     ylab="线性代数",
     xlim=c(0,100),
     ylim=c(0,100),
     xaxs="i", #Set x axis style as internal
     yaxs="i", #Set y axis style as internal
     col="red", #Set the color of plotting symbol to red 
     pch=19, #Set the plotting symbol to filled dots
     family ="Arial Unicode MS") 

 

连线图

a=c(2,3,4,5,6)

b=c(4,7,8,9,12)

plot(a,b,type = "l")

 

多条曲线的效果 

plot(rain$Tokyo,type="l",col="red", ylim=c(0,300),
main="Monthly Rainfall in major cities", xlab="Month of Year",
ylab="Rainfall (mm)",
lwd=2) 
lines(rain$NewYork,type="l",col="blue",lwd=2)
lines(rain$London,type="l",col="green",lwd=2)
lines(rain$Berlin,type="l",col="orange",lwd=2)

 

密度图

plot(density(rnorm(1000)))

热力图

heatmap()

向日葵散点图 

sunflowerplot(iris[,3:4],col="gold",seg.col="gold")

散点图集 

pairs(iris[,1:4])

 

par(mfrow=c(3,1))
plot(x1,x2)
plot(x2,x3)
plot(x3,x1)

 

绘图参数

help(par)

colors()

--待修改

三维散点图 

安装scatterplot3d 包 

scatterplot3d(x[2:4]) 

 

三维作图 

 

调和曲线图 

调和曲线用于聚类判断非常方便 

 

地图 

安装maps包 

map("state", interior = FALSE) 

map("state", boundary = FALSE, col="red", add = TRUE) 

map('world', fill = TRUE,col=heat.colors(10)) 

 

社交数据可视化 


 

 

 

 

 

 

posted @ 2016-09-01 22:01  kyo.stone  阅读(264)  评论(0)    收藏  举报