行列名称
colnames(iris) 花函数
matrix构造矩阵
cbind列拼
rbind行拼
行列名称
colnames(iris) 花函数
matrix构造矩阵
cbind列拼
rbind行拼
www.rstudio.com
桌面版本
选操作系统
优势:
脚本编辑
控制台:交互式命令
工作空间:变量,历史命令
文件系统
rstudio有代码提示
rattle,图形化操作界面
R区分大小写
1+1
2+2
在脚本区run
sin(30)
sqrt(12)
帮助系统
ploth绘图函数
help(plot)
example("plot")直接示例
工作空间
getwd() setwd()
文件操作,完整路径
setwd('c/user/zhang')
R包
library
函数.lib
包的安装
install.packages('arules')
安装过程中需要联网。
tools options cran mirror选中国清华大学镜像
调用library('arules')
使用时先加载detach
更新包
installed.package()
已经安装好的包
.library()
十四课时:
在编程的时候,会经常用到的操作就是数据的读取。怎么读取数据呢?常用两种方式,一种是本地数据的读入和读出,另一种是网络数据的排序。
一:本地数据的读取
1.首先,一般数据存储于txt,excel,更大型的数据是使用数据库储存。在R语言中,第一个是读入加载的函数包里的数据 :library(learningr)
learningr:是数据包
library(learningr)
deer_file<-system.file(
'extdata',%文件夹
'RedDeerEndocranialvolume.dlm',%函数包里的数据
package='learningr'%函数包
)
deer_data<-read.table(deer_file,header=TRUE,fill=TRUE)
deer_data
基本赋值语句
变量名<-
x<-8
1.数据结构:向量、矩阵、数组、数据框、列表
2.判定用is+转换类型名称,转换用as+转换类型名称
3.length用于测量向量长度,mode
输入help(‘函数名’)
Rstudio区分大小写
基本函数默认直接用
不知道某个函数的用法可以输入help(‘函数名’)例如:help(‘plot’)
Rstudio下载地址:http://www.rstudio.com/
getwd()来查看当前的工作目录
setwd()设定当前目录
安装包install.packages('包的名字')
install.packages('arules')
library()
课堂练习
2+1
2*3
12^2+21^2
sin(40)+cos(30)
1.class用于查看数据类型
2.符号c用于创造向量
3.cbind 按列合并 rbind 按行合并
4.函数data frame 构造数据框
5.函数list
R
iris#调用鸢尾花样本集
library(rpart)#加载决策树包
par(family='STXihei')#图形设置避免出现中文乱码
ct<-rpart.control(minsplit = 20,cp=0.1)
#minsplit表示每个节点中所含样本值,cp为复杂度参数,表示建模过程中仅仅保留模拟拟合度提升0.1及以上的节点
fit<-rpart(Species~Sepal.Length+Sepal.Width+Petal.Length+Petal.Width,data = iris,method = "class",control = ct)
#第一种分类
par(mfrow=c(1,3))
plot(fit)
text(fit,use.n=T,all=T,cex = 0.9)
#查看比较
iris[iris$Petal.Length<2.45,]
x<-iris[iris$Petal.Length>=2.45 & iris$Petal.Width<1.75,'Species']
table(x)
##############鸢尾花神经网络识别####################
#随机抽取80%样本作为训练样本
library(nnet)
iris
trIn<-c(sample(1:50,40),sample(51:100,40),sample(101:150,40))
trainD<-iris[trIn,]#训练样本集
testD<-iris[-trIn,]#测试样本集
ide<-class.ind(iris$Species)#类别识别预处理
#构建神经网络,模型训练
#Net<-nnet(trainD[,-5],ide[trIn,],size =5,softmax = TRUE,decay=0.05)
Net<-nnet(trainD[,-5],ide[trIn,],size =5,softmax = TRUE)
#测试神经网络
Result<-predict(Net,testD[,-5],type='class')
#分类结果
OUTCOME<-table(testD$Species,Result)
txt<-readLines("/Users/git/Desktop/Word.txt")
txt<-strsplit(txt,' ')
class(txt)
txt=unlist(txt)
class(txt)
txt<-chartr("A-Z", "a-z", txt)
txt1<-gsub('[[:punct:]]\\w?','',txt)
txt2<-table(txt1)
txt2<-sort(txt2,decreasing = T)
barplot(txt2[1:100])
sum(txt2)
x<-1:20
Num<-function(x){
stopifnot(is.numeric(x))
k<-sum(x%%2==0)
return(k)
}
Num(x)
x<-1:20
Num<-function(x){
k=0
stopifnot(is.numeric(x))
for (i in 1:length(x)) {
if(i%%2==0){
k=k+1
}
}
return(k)
}
Num(x)
repeat,break
a<-0
repeat{
a<-a+1
if(a%%2==1){
print("I LOVE YOU")
}else{print("I LOVE HIM")}
if(a>=100){
break
}
}
while循环语句:while(){},条件判断
a<-0
calcu<-0
while (a<=10) {
a=a+1
if(a%%2==1){
calcu<-calcu+1
}#求奇数个数
}
for循环语句:
x<-1:200
count<-function(x){
count1<-0
for(i in 1:length(x)){
if(x[i]%%2==1){
count1<-count1+1
}
}
return(count1)
}
count(x)
count2<-function(x){
cou<-sum(x%%2==1)
return((cou))
}
count2(x)
控制流:if-else,if(){}else();switch(case,case1,case2)
apply函数,非常实用!apply(iris[,-5],1,mean)
sapply(iris[,-5],mean),按列操作
sqldf()