------------快捷键
ctrl+R 脚本可直接运行
控制台:
Up/down 回忆之前的命令
Ctrl+Up 回顾命令列表(可先输入前缀进行查找)
焦点:
ctrl+1 移动焦点到source编辑器
ctrl+2 移动焦点到console
ctrl+L 清理控制台
Esc 中断R
Ctrl+Shift+N 创建新文件
Ctrl+O 打开已有文件
Ctrl+S 保存活动文档
Ctrl+W 关闭活动文档
Ctrl+Shift+W 关闭所有打开文档
Ctrl+. go to file/function
Ctrl+Shift+O source a file
Ctrl+F 打开查找与替换栏
Ctrl+Shift+U RStudio可以在source编辑器中分析一组选择的代码,并自动将其转化成再次使用的函数。任何选择中的"free"变量(选择引用对象但不创建)将转化为函数参数。
Ctrl+Shift+C 注释或取消注释
Ctrl+D 删除行
Ctrl+Enter 执行一行代码,光标自动跳转下一行(也可选中后执行多行代码)
Ctrl+Shift+P 再次运行相同的选择/或之前的操作[包括增删该等]
Ctrl+Shift+Enter 运行整个文件
Ctrl+Alt+R 运行当前文档
Ctrl+Alt+E 从当前行运行到结尾
Ctrl+Alt+C 运行当前块
Ctrl+Alt+N 运行下一块
Alt+L 代码折叠
Alt+Shift+L 折叠代码展开
Alt+A 折叠全部
Alt+Shift+J 代码段间漫游
F2 或 Ctrl+Click 访问函数定义
Ctrl+F9/Ctrl+F10 Back/Forward
删除所有对象: rm(list=ls(all=TRUE))
列出查找路径,即当前使用的程序包: search()
加载程序包 library('MASS')
安装程序包 install.packages('packagename')
R函数帮助 help.search('Fun_name')
或help.search(Fun_name)
或?Fun_name
搜索帮助(在整个程序包中查找) help.search('Func_name')
??Fun_name
模糊查找对象 apropos('Func_name')
----------R下载包更换镜像语句
local(
{
r <- getOption("repos")
r["CRAN"] <- "http://mirrors.ustc.edu.cn/CRAN/"
options(repos=r)
})
install.packages("arulesSequences")
library(arulesSequences)
备用镜像:
http://mirror.bjtu.edu.cn/cran/
http://mirrors.ustc.edu.cn/CRAN/
http://mirrors.tuna.tsinghua.edu.cn/CRAN/
http://mirrors.xmu.edu.cn/CRAN/
----------安装包
install.packages('包名字')
install.packages('包名字', lib='安装目录', repos='包所在的网址')
install.packages('/home/R/assertthat_0.1.tar.gz')
R CMD INSTALL 包文件的完整路径
---------查看所有已经安装的包
.packages(all.available=T)
---------设定和查看工作目录
设定工作目录
setwd()
setwd('D:\\R')
注意:用'\\' 代替原本的'\'
查看工作目录
getwd()
------------查看前10行
a[1:10,]
-------------查看行数
nrow(a)
-----------查看前10列
iris[,1:5]
-----------------查看开始部分
> head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
-----------------查看开始的一行
> head(iris,1)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
-----------------查看结束部分
> tail(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
145 6.7 3.3 5.7 2.5 virginica
146 6.7 3.0 5.2 2.3 virginica
147 6.3 2.5 5.0 1.9 virginica
148 6.5 3.0 5.2 2.0 virginica
149 6.2 3.4 5.4 2.3 virginica
150 5.9 3.0 5.1 1.8 virginica
使用stringsAsFactors=F来防止data.frame把向量转为factor
apache = data.frame(httpCode=c(200,200,200,404,404,500),
time=c(100,111,210,10,10,500),
api=c('index','index','logout','show','show','index'),
stringsAsFactors=F)
---------查看数据的范围
a=c(1,2,3,4,5,9,4,1,2,3,5,4,1)
range(a)
[1] 1 9
-------------查看包中的函数
library(topicmodels)
ls("package:包的名称")
library(topicmodels)
ls("package:topicmodels")
-----------查找包的帮助信息
??"ggplot2"
---------------查看数据框基本信息
str(ad_msg)
----------------which函数
which.max(a):最大值的项的下标;
which.min(a): 最小值的项的下标;
a[which.max(a)]:最大值;
a[which.min(a)]:最小值;
which(a==2):向量的项为2的下标;
a[which(a==2)]:向量项为2的值;
which(a>5):向量的项大于5的下标;
a[which(a>5)]:向量的项大于5的值;
> a=c(2,3,4,2,5,1,6,3,2,5,8,5,7,3)
> which.max(a)
[1] 11
> which.min(a)
[1] 6
> a[which.max(a)]
[1] 8
> a[which.min(a)]
[1] 1
> which(a==2)
[1] 1 4 9
> a[which(a==2)]
[1] 2 2 2
> which(a>5)
[1] 7 11 13
> a[which(a>5)]
[1] 6 8 7
----------------nchar()和length()
x为字符串向量,返回每个字符串的长度。
> nchar("hello world")
[1] 11
> nchar(c("I love R language", "R is free"))
[1] 18 9
length()计算向量的长度
> length("hello world")
[1] 1
> length(c("I love R language", "R is free"))
[1] 2
------------正则替换
> a<-"AB(CD)EFG(I)JK"
> gsub("[(.*)]", "", a)
[1] "ABCDEFGIJK"
> gsub("\\(.*?\\)","",a)
[1] "ABEFGJK"
--------------R ceiling、round & floor
ceiling返回对应数字的'天花板'值,就是不小于该数字的最小整数
> a <-c (1,2.5,3.2,3.5,3.6)
> ceiling(a)
[1] 1 3 4 4 4
floor与ceiling相对,返回'地板'值,即不大于该数字的最大值
> floor(a)
[1] 1 2 3 3 3
trunc的特性是'向零截取', 也就是说对于一个数字a,它将数轴分成两侧,trunc(a)将返回数轴上包含数字0的那一侧离a最近的那个整数,例如:
> a
[1] 1.0 2.5 3.2 3.5 3.6
> trunc(a)
[1] 1 2 3 3 3
> b<-c(-1,-2.5,-3.2,-3.5,-3.6)
> trunc(b)
[1] -1 -2 -3 -3 -3
round是R里的'四舍五入'函数,具体的规则采用banker's rounding,即四舍六入五留双规则(wiki)
round的原型是round(x, digits = 0), digits设定小数点位置,默认为零即小数点后零位(取整)。
> c<-c(1.4,1.6,1.5,2.5,2.51)
> round(c)
[1] 1 2 2 2 3
---- 看数据类型
> a<-c(1,1)
> a
[1] 1 1
> class(a)
[1] "numeric"
> a<-c(1,1)
> a
[1] 1 1
> unique(a)
[1] 1
---------替换缺失值
A[A == 0] <- y
--------------添加列
> str(iris)
'data.frame': 150 obs. of 5 variables:
$ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
> iris$a<-iris$Sepal.Length/iris$Sepal.Width
> str(iris)
'data.frame': 150 obs. of 6 variables:
$ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
$ a : num 1.46 1.63 1.47 1.48 1.39 ...
-----------数据标准化
> iris[1:6,1:2]
Sepal.Length Sepal.Width
1 5.1 3.5
2 4.9 3.0
3 4.7 3.2
4 4.6 3.1
5 5.0 3.6
6 5.4 3.9
> iris2<- scale(iris[,1:2])
> head(iris2)
Sepal.Length Sepal.Width
[1,] -0.8976739 1.01560199
[2,] -1.1392005 -0.13153881
[3,] -1.3807271 0.32731751
[4,] -1.5014904 0.09788935
[5,] -1.0184372 1.24503015
[6,] -0.5353840 1.93331463
---------------表格合并
列合并 cbind
> iris4<-cbind(iris,iris2)
> head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species a
1 5.1 3.5 1.4 0.2 setosa 1.457143
2 4.9 3.0 1.4 0.2 setosa 1.633333
3 4.7 3.2 1.3 0.2 setosa 1.468750
4 4.6 3.1 1.5 0.2 setosa 1.483871
5 5.0 3.6 1.4 0.2 setosa 1.388889
6 5.4 3.9 1.7 0.4 setosa 1.384615
> head(iris4)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species a Sepal.Length Sepal.Width
1 5.1 3.5 1.4 0.2 setosa 1.457143 -0.8976739 1.01560199
2 4.9 3.0 1.4 0.2 setosa 1.633333 -1.1392005 -0.13153881
3 4.7 3.2 1.3 0.2 setosa 1.468750 -1.3807271 0.32731751
4 4.6 3.1 1.5 0.2 setosa 1.483871 -1.5014904 0.09788935
5 5.0 3.6 1.4 0.2 setosa 1.388889 -1.0184372 1.24503015
6 5.4 3.9 1.7 0.4 setosa 1.384615 -0.5353840 1.93331463
行合并 rbind
> a<-iris[1:2,]
> a
Sepal.Length Sepal.Width Petal.Length Petal.Width Species a
1 5.1 3.5 1.4 0.2 setosa 1.457143
2 4.9 3.0 1.4 0.2 setosa 1.633333
> b<-iris[3:4,]
> b
Sepal.Length Sepal.Width Petal.Length Petal.Width Species a
3 4.7 3.2 1.3 0.2 setosa 1.468750
4 4.6 3.1 1.5 0.2 setosa 1.483871
> c<-rbind(a,b)
> c
Sepal.Length Sepal.Width Petal.Length Petal.Width Species a
1 5.1 3.5 1.4 0.2 setosa 1.457143
2 4.9 3.0 1.4 0.2 setosa 1.633333
3 4.7 3.2 1.3 0.2 setosa 1.468750
4 4.6 3.1 1.5 0.2 setosa 1.483871
-------------------提取某种规则的数据
> c
Sepal.Length Sepal.Width Petal.Length Petal.Width Species a
1 5.1 3.5 1.4 0.2 setosa 1.457143
2 4.9 3.0 1.4 0.2 setosa 1.633333
3 4.7 3.2 1.3 0.2 setosa 1.468750
4 4.6 3.1 1.5 0.2 setosa 1.483871
> c[c[,"Petal.Length"]=="1.4",]
Sepal.Length Sepal.Width Petal.Length Petal.Width Species a
1 5.1 3.5 1.4 0.2 setosa 1.457143
2 4.9 3.0 1.4 0.2 setosa 1.633333
> c[c[,"Petal.Length"]=="1.4"&c[,"Sepal.Length"]=='5.1',]
Sepal.Length Sepal.Width Petal.Length Petal.Width Species a
1 5.1 3.5 1.4 0.2 setosa 1.457143
--------------------查看分类变量的信息
> table(iris$Species)
setosa versicolor virginica
50 50 50
> unique(iris$Species)
[1] setosa versicolor virginica
Levels: setosa versicolor virginica
-------------dim用法
> a<-c(1:20)
> a
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
> dim(a)
NULL
> dim(a)<-c(2,10)
> a
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 3 5 7 9 11 13 15 17 19
[2,] 2 4 6 8 10 12 14 16 18 20
> dim(a)<-c(10,2)
> a
[,1] [,2]
[1,] 1 11
[2,] 2 12
[3,] 3 13
[4,] 4 14
[5,] 5 15
[6,] 6 16
[7,] 7 17
[8,] 8 18
[9,] 9 19
[10,] 10 20
> dim(a)
[1] 10 2
---------------做列联表
> b<-unlist(sapply(c("setosa", "versicolor"), function(x)dim(iris[iris[,"Species"]==x,])[1]))
> a<-unlist(sapply(c("setosa", "versicolor"), function(x)dim(iris[iris[,"Species"]==x,])[1]))
> a
setosa versicolor
50 50
> b
setosa versicolor
50 50
> cbind(a,b)
a b
setosa 50 50
versicolor 50 50
> data.frame(a=a,b=b)
a b
setosa 50 50
versicolor 50 50
> data.frame(a,b)
a b
setosa 50 50
versicolor 50 50
------------rep()函数
Rep(a,times= ),后边的数是重复前面数的次数。就是将3重复2次,将5重复4次。
> rep(3,5)
[1] 3 3 3 3 3
> rep(3,times=5)
[1] 3 3 3 3 3
> rep(c(1,2),c(5,5))
[1] 1 1 1 1 1 2 2 2 2 2
---------------:sort(),rank(),order()
sort(x)是对向量x进行排序,返回值排序后的数值向量。rank()是求秩的函数,它的返回值是这个向量中对应元素的“排名”。而order()的返回值是对应“排名”的元素所在向量中的位置。
> x<-c(97,93,85,74,32,100,99,67)
> sort(x)
[1] 32 67 74 85 93 97 99 100
> order(x)
[1] 5 8 4 3 2 1 7 6
> rank(x)
[1] 6 5 4 3 1 8 7 2
---------------求累积和
cumsum(x) 求累计和
a=c(1,2,3,4,5,9,4,1,2,3,5,4,1)
> cumsum(a)
[1] 1 3 6 10 15 24 28 29 31 34 39 43 44
------------Rstudio解决中文乱码问题
name=names(futures_user)
name<-iconv(name,"UTF-8","gbk")
----R中文乱码问题
dbSendQuery(con,"SET NAMES utf8;")
windows
dbSendQuery(con,"SET NAMES GBK;")
-------------------选取字段
d[dname=="de"|dname=="ji",]