学习R最重要的, 其实也是学习任何语言都可能是最重要的.
在线帮助, 和一些小技巧.
使用ls()函数可以查看当前环境中的对象
使用ls.str()可以查看当前环境中对象的详细信息.
ls和ls.str都支持通配符, 可以列出符合规则表达式的对象.
使用rm删除对象
结合rm()和ls()可以删除所有对象
结合通配符, 可以删除符合通配符的对象.
默认情况下, help只查询载入当前内存环境的包的信息, 如果你要查的命令或函数不在内存中, 需要给help加参数.
然后, 指定包名就可以查看其帮助了.
如果要查找帮助内容中包含某字符的函数, 可以使用help.search, 这个命令同样只在当前内存环境中查找, 如果要在所有包中查找, 需要加rebuild=TRUE选项.
另外还有一个小技巧 :
> a <- 1:10
> a
[1] 1 2 3 4 5 6 7 8 9 10
> ls()
[1] "a"
> b <- 10:19
> c <- data.frame(a,b)
> ls()
[1] "a" "b" "c"
使用ls.str()可以查看当前环境中对象的详细信息.
> ls.str()
a : int [1:10] 1 2 3 4 5 6 7 8 9 10
b : int [1:10] 10 11 12 13 14 15 16 17 18 19
c : 'data.frame': 10 obs. of 2 variables:
$ a: int 1 2 3 4 5 6 7 8 9 10
$ b: int 10 11 12 13 14 15 16 17 18 19
ls和ls.str都支持通配符, 可以列出符合规则表达式的对象.
> ls(pattern= "a|b")
[1] "a" "b"
> ls.str(pattern= "a|b")
a : int [1:10] 1 2 3 4 5 6 7 8 9 10
b : int [1:10] 10 11 12 13 14 15 16 17 18 19
使用rm删除对象
> rm(a)
> a
Error: object 'a' not found
结合rm()和ls()可以删除所有对象
> rm(list=ls())
结合通配符, 可以删除符合通配符的对象.
rm(list=ls(pat="^m"))
接下来看看帮助相关的命令.
?cmd
help(cmd)
help("cmd")
以上三种都可以.
但是help("cmd")更通用, 例如要查询某些特殊符号的帮助, 例如乘法符号, 必须使用help("*"), 其他两种都不行.
> ?*
Error: unexpected '*' in "?*"
> help(*)
Error: unexpected '*' in "help(*"
> help("*")
Arithmetic package:base R Documentation
Arithmetic Operators
Description:
These unary and binary operators perform arithmetic on numeric or
complex vectors (or objects which can be coerced to them).
Usage:
+ x
- x
x + y
x - y
x * y
x / y
x ^ y
x %% y
x %/% y
................
默认情况下, help只查询载入当前内存环境的包的信息, 如果你要查的命令或函数不在内存中, 需要给help加参数.
try.all.package = TRUE
例如:
> help("bs")
No documentation for ‘bs’ in specified packages and libraries:
you could try ‘??bs’
> help("bs", try.all.package=TRUE)
Help for topic ‘bs’ is not in any loaded package but can be found in
the following packages:
Package Library
splines /usr/lib64/R/library
然后, 指定包名就可以查看其帮助了.
> help("bs", package = "splines")
bs package:splines R Documentation
B-Spline Basis for Polynomial Splines
Description:
Generate the B-spline basis matrix for a polynomial spline.
Usage:
bs(x, df = NULL, knots = NULL, degree = 3, intercept = FALSE,
Boundary.knots = range(x))
...........
如果要查找帮助内容中包含某字符的函数, 可以使用help.search, 这个命令同样只在当前内存环境中查找, 如果要在所有包中查找, 需要加rebuild=TRUE选项.
> help.search("tree", rebuild = TRUE)
Help files with alias or concept or title matching ‘tree’ using regular
expression matching:
cluster::pltree Clustering Trees - Generic Function
cluster::pltree.twins Clustering Tree of a Hierarchical Clustering
codetools::showTree Print Lisp-Style Representation of R Expression
datasets::Loblolly Growth of Loblolly pine trees
datasets::Orange Growth of Orange Trees
datasets::treering Yearly Treering Data, -6000-1979
datasets::trees Girth, Height and Volume for Black Cherry Trees
grid::current.viewport
Get the Current Grid Viewport (Tree)
grid::grob Create Grid Graphical Objects, aka "Grob"s
grid::grid.reorder Reorder the children of a gTree
grid::viewport Create a Grid Viewport
grid::pushViewport Maintaining and Navigating the Grid Viewport
Tree
MASS::Sitka Growth Curves for Sitka Spruce Trees in 1988
MASS::Sitka89 Growth Curves for Sitka Spruce Trees in 1989
nlme::Spruce Growth of Spruce Trees
rpart::rpart Recursive Partitioning and Regression Trees
rpart::rpart.object Recursive Partitioning and Regression Trees
Object
rpart::snip.rpart Snip Subtrees of an Rpart Object
stats::cutree Cut a Tree into Groups of Data
stats::dendrogram General Tree Structures
tcltk::TkWidgets Tk widgets
Type '?PKG::FOO' to inspect entries 'PKG::FOO', or 'TYPE?PKG::FOO' for
entries like 'PKG::FOO-TYPE'.
或者你可以使用两个问号, 和help.search效果一样.
如??tree
使用函数apropos则能找出所有在名字中含有指定字符串的函数,但只会
在被载入内存中的包中进行搜索.
例如, 包含help字符串的函数 :
> apropos("help")
[1] "help" "help.request" "help.search" "help.start"
另外还有一个小技巧 :
在R中操作时, 使用TAB键可以补齐.
好了, 就写到这里, 要善于使用帮助.
[参考]
?
help
help.search
ls
ls.str
rm
apropos
TAB