1. quit console
> q() or
>quit()
2. help
> help(solve) or
> ?solve
> help.start()
3. help.search() --search for help
>help.search(solve) or
> ??solve
4. show examples of functions or topic
> example(topic) ---example(solve)
5. batch run r code, that is, running r by code of file
> source("commands.R")
6. redirect the output to files
> sink("record.lis") --redicrct the console output
to the file named "record.lis"
> sink()
--restore the output to the
console
7. display the objects currently stored in R
> objects() or
>ls()
8. remove objects currrently stored in R
> rm(x,
y, z, ink, junk, temp, foo, bar)
9.vectors assignment or creation ,
> x
<- c(10.4, 5.6, 3.1, 6.4, 21.7) or --c() is a function to
generate a vector
> assign("x", c(10.4, 5.6, 3.1, 6.4, 21.7))
or
> c(10.4, 5.6, 3.1, 6.4, 21.7) -> x
--manipulatation of variables
> 1/x
> y <- c(x, 0, x)
> v
<- 2*x + y + 1
10. basic function
x is vectore
>min(x)
>max(x)
>range(x)
>length(x)
log
exp
sin
cos
tan
sqrt
sum
prod ---production of all elements
var ---sample variance = sum((x-mean(x))^2) / (length(x)-1)
---If the argument to var() is an n-by-p matrix the value is a p-by-p sample covariance matrix got by regarding the rows as independent p-variate sample vectors.
sort ---sort the vectore in increasing order
pmax --parallel maximum and minimum max and min, operating on several vectors
pmin
sqrt ---can compute the sqare root of complex number
11. generating sequences
>1:30
>n<- 10
>1:n-1
>1:(n-1)
12. generate sequences by seq() function
> seq(-5, 5, by=.2) -> s3
> s4 <- seq(length=51, from=-5, by=.2)
parameters: seq(from, to, by, length, along)
13. replication function
> s5 <- rep(x,
times=5)
> s6 <- rep(x, each=5)
> temp <- x >
13
The logical operators are <, <=, >, >=, == for exact equality
and != for inequality. In addition if c1 and c2 are logical expressions, then c1
& c2 is their intersection (鈥渁nd鈥?, c1 | c2 is their union (鈥渙r鈥?, and !c1
is the negation of c1. Logical vectors may be used in ordinary arithmetic, in
which case they are coerced into numeric vectors, FALSE becoming 0 and TRUE
becoming 1. However there are situations where logical vectors and their coerced
numeric counterparts are not equivalent, for example see the next
subsection.
> z <- c(1:3,NA); ind <- is.na(z)
>
0/0
> Inf - Inf
In summary, is.na(xx) is TRUE both for NA and NaN
values. To differentiate these, is.nan(xx) is only TRUE for NaNs. Missing values
are sometimes printed as <NA> when character vectors are printed without
quotes.
> labs <- paste(c("X","Y"), 1:10, sep="")
> y <- x[!is.na(x)]
> (x+1)[(!is.na(x)) & x>0]
-> z
> x[1:10]
> c("x","y")[rep(c(1,2,2,1),
times=4)]
> y <- x[-(1:5)]
> fruit <- c(5,
10, 1, 20)
> names(fruit) <- c("orange", "banana", "apple",
"peach")
> lunch <- fruit[c("apple","orange")]
>
x[is.na(x)] <- 0
> y[y < 0] <- -y[y < 0]
> y
<- abs(y)
> z <- 0:9
> digits <-
as.character(z)
> d <- as.integer(digits)
> e <-
numeric()
> e[3] <- 17
> alpha <- alpha[2 * 1:5]
> length(alpha) <- 3
> attr(z, "dim") <- c(10,10)
> winter
> unclass(winter)
> state <- c("tas",
"sa", "qld", "nsw", "nsw", "nt", "wa", "wa",
> statef <-
factor(state)
> statef
> levels(statef)
>
incomes <- c(60, 49, 40, 61, 64, 60, 59, 54, 62, 69, 70, 42, 56,
>
incmeans <- tapply(incomes, statef, mean)
> stderr <-
function(x) sqrt(var(x)/length(x))
> incster <- tapply(incomes,
statef, stderr)
> incster
> dim(z) <- c(3,5,100)
> x <- array(1:20, dim=c(4,5)) # Generate a 4 by 5 array.
>
x
> i <- array(c(1:3,3:1), dim=c(3,2))
> i
# i is a 3 by 2 index array.
> x[i]
# Extract those elements
> x[i] <- 0 #
Replace those elements by zeros.
> x
>
> Xb
<- matrix(0, n, b)
> Xv <- matrix(0, n, v)
> ib
<- cbind(1:n, blocks)
> iv <- cbind(1:n, varieties)
> Xb[ib] <- 1
> Xv[iv] <- 1
> X <- cbind(Xb,
Xv)
> N <- crossprod(Xb, Xv)
> N <- table(blocks,
varieties)
> Z <- array(data_vector, dim_vector)
> Z
<- array(h, dim=c(3,4,2))
> Z <- h ; dim(Z) <- c(3,4,2)
> Z <- array(0, c(3,4,2))
> D <- 2*A*B + C + 1
> ab <- a %o% b
> ab <- outer(a, b, "*")
> f
<- function(x, y) cos(y)/(1 + x^2)
> z <- outer(x, y, f)
> d <- outer(0:9, 0:9)
> fr <- table(outer(d, d, "-"))
> plot(as.numeric(names(fr)), fr, type="h",
> B <- aperm(A,
c(2,1))
> A * B
> A %*% B
> x %*% A %*% x
> b <- A %*% x
> solve(A,b)
> ev <-
eigen(Sm)
> evals <- eigen(Sm
values
> absdetM <- prod(svd(M
d)
> ans <- lsfit(X, y)
> Xplus <- qr(X)
> b <- qr.coef(Xplus, y)
> fit <- qr.fitted(Xplus, y)
> res <- qr.resid(Xplus, y)
> X <- cbind(arg_1, arg_2,
arg_3, ...)
> X <- cbind(1, X1, X2)
> vec <-
as.vector(X)
> vec <- c(X)
> statefr <-
table(statef)
> statefr <- tapply(statef, statef, length)
> factor(cut(incomes, breaks = 35+10*(0:7))) -> incomef
>
table(incomef,statef)
> Lst <- list(name="Fred", wife="Mary",
no.children=3,
> nam > Lst <- list(name_1=object_1, ...,
name_m=object_m)
> Lst[5] <- list(matrix=Mat)
>
list.ABC <- c(list.A, list.B, list.C)
> accountants <-
data.frame(home=statef, loot=incomes, shot=incomef)
>
attach(lentils)
> u <- v+w
> lentils"
alt="component_name
> x <- "name"; Lst[[x]]
> Lst <-
list(name_1=object_1, ..., name_m=object_m)
> Lst[5] <-
list(matrix=Mat)
> list.ABC <- c(list.A, list.B, list.C)
> accountants <- data.frame(home=statef, loot=incomes, shot=incomef)
> attach(lentils)
> u <- v+w
> lentils"
title="component_name
> x <- "name"; Lst[[x]]
> Lst
<- list(name_1=object_1, ..., name_m=object_m)
> Lst[5] <-
list(matrix=Mat)
> list.ABC <- c(list.A, list.B, list.C)
> accountants <- data.frame(home=statef, loot=incomes, shot=incomef)
> attach(lentils)
> u <- v+w
> lentils" border="0"
class="latex" /> u <- v+w
> detach()
>
attach(any.old.list)
> search()
> search()
>
ls(2)
> detach("lentils")
> search()
>
HousePrice <- read.table("houses.data")
> HousePrice <-
read.table("houses.data", header=TRUE)
> inp <- scan("input.dat",
list("",0,0))
> label <- inp[[1]]; x <- inp[[2]]; y <-
inp[[3]]
> inp <- scan("input.dat", list(id="", x=0, y=0))
> label <- in x; y <- in
> xnew <-
edit(xold)
> xnew <- edit(data.frame())
One convenient use of R
is to provide a comprehensive set of statistical tables. Functions are provided
to evaluate the cumulative distribution function P(X <= x), the probability
density function and the quantile function (given q, the smallest x such that
P(X <= x) > q), and to simulate from the distribution.
> ##
2-tailed p-value for t distribution
> 2*pt(-2.43, df = 13)
> ## upper 1% point for an F(2, 7) distribution
> qf(0.01, 2, 7,
lower.tail = FALSE)
> attach(faithful)
>
summary(eruptions)
> fivenum(eruptions)
>
stem(eruptions)
> hist(eruptions)
> hist(eruptions,
seq(1.6, 5.2, 0.2), prob=TRUE)
> lines(density(eruptions,
bw=0.1))
> rug(eruptions) # show the actual data points
>
plot(ecdf(eruptions), do.points=FALSE, verticals=TRUE)
> long <-
eruptions[eruptions > 3]
> plot(ecdf(long), do.points=FALSE,
verticals=TRUE)
> x <- seq(3, 5.4, 0.01)
> lines(x,
pnorm(x, mean=mean(long), sd=sqrt(var(long))), lty=3)
>
shapiro.test(long)
> ks.test(long, "pnorm", mean = mean(long), sd =
sqrt(var(long)))
> t.test(A, B)
> var.test(A, B)
> t.test(A, B, var.equal=TRUE)
> wilcox.test(A, B)
>
plot(ecdf(A), do.points=FALSE, verticals=TRUE, xlim=range(A, B))
>
plot(ecdf(B), do.points=FALSE, verticals=TRUE, add=TRUE)
> ks.test(A,
B)
> if (expr_1) expr_2 else expr_3
> for (name in expr_1)
expr_2
> xc <- split(x, ind)
> yc <- split(y,
ind)
> for (i in 1:length(yc)) {
> repeat expr
> while (condition) expr
> name <- function(arg_1, arg_2, ...)
expression
> twosam <- function(y1, y2) {
> tstat <-
twosam(data" alt="y
> X <- matrix(scan("light.dat", 0), ncol=5,
byrow=TRUE)
> xnew <- edit(xold)
> xnew <-
edit(data.frame())
One convenient use of R is to provide a comprehensive set
of statistical tables. Functions are provided to evaluate the cumulative
distribution function P(X <= x), the probability density function and the
quantile function (given q, the smallest x such that P(X <= x) > q), and
to simulate from the distribution.
> ## 2-tailed p-value for t
distribution
> 2*pt(-2.43, df = 13)
> ## upper 1% point
for an F(2, 7) distribution
> qf(0.01, 2, 7, lower.tail = FALSE)
> attach(faithful)
> summary(eruptions)
>
fivenum(eruptions)
> stem(eruptions)
> hist(eruptions)
> hist(eruptions, seq(1.6, 5.2, 0.2), prob=TRUE)
>
lines(density(eruptions, bw=0.1))
> rug(eruptions) # show the actual
data points
> plot(ecdf(eruptions), do.points=FALSE,
verticals=TRUE)
> long <- eruptions[eruptions > 3]
>
plot(ecdf(long), do.points=FALSE, verticals=TRUE)
> x <- seq(3,
5.4, 0.01)
> lines(x, pnorm(x, mean=mean(long), sd=sqrt(var(long))),
lty=3)
> shapiro.test(long)
> ks.test(long, "pnorm", mean
= mean(long), sd = sqrt(var(long)))
> t.test(A, B)
>
var.test(A, B)
> t.test(A, B, var.equal=TRUE)
>
wilcox.test(A, B)
> plot(ecdf(A), do.points=FALSE, verticals=TRUE,
xlim=range(A, B))
> plot(ecdf(B), do.points=FALSE, verticals=TRUE,
add=TRUE)
> ks.test(A, B)
> if (expr_1) expr_2 else
expr_3
> for (name in expr_1) expr_2
> xc <- split(x,
ind)
> yc <- split(y, ind)
> for (i in 1:length(yc))
{
> repeat expr
> while (condition) expr
> name
<- function(arg_1, arg_2, ...) expression
> twosam <-
function(y1, y2) {
> tstat <- twosam(data" title="y
> X
<- matrix(scan("light.dat", 0), ncol=5, byrow=TRUE)
> xnew <-
edit(xold)
> xnew <- edit(data.frame())
One convenient use of R
is to provide a comprehensive set of statistical tables. Functions are provided
to evaluate the cumulative distribution function P(X <= x), the probability
density function and the quantile function (given q, the smallest x such that
P(X <= x) > q), and to simulate from the distribution.
> ##
2-tailed p-value for t distribution
> 2*pt(-2.43, df = 13)
> ## upper 1% point for an F(2, 7) distribution
> qf(0.01, 2, 7,
lower.tail = FALSE)
> attach(faithful)
>
summary(eruptions)
> fivenum(eruptions)
>
stem(eruptions)
> hist(eruptions)
> hist(eruptions,
seq(1.6, 5.2, 0.2), prob=TRUE)
> lines(density(eruptions,
bw=0.1))
> rug(eruptions) # show the actual data points
>
plot(ecdf(eruptions), do.points=FALSE, verticals=TRUE)
> long <-
eruptions[eruptions > 3]
> plot(ecdf(long), do.points=FALSE,
verticals=TRUE)
> x <- seq(3, 5.4, 0.01)
> lines(x,
pnorm(x, mean=mean(long), sd=sqrt(var(long))), lty=3)
>
shapiro.test(long)
> ks.test(long, "pnorm", mean = mean(long), sd =
sqrt(var(long)))
> t.test(A, B)
> var.test(A, B)
> t.test(A, B, var.equal=TRUE)
> wilcox.test(A, B)
>
plot(ecdf(A), do.points=FALSE, verticals=TRUE, xlim=range(A, B))
>
plot(ecdf(B), do.points=FALSE, verticals=TRUE, add=TRUE)
> ks.test(A,
B)
> if (expr_1) expr_2 else expr_3
> for (name in expr_1)
expr_2
> xc <- split(x, ind)
> yc <- split(y,
ind)
> for (i in 1:length(yc)) {
> repeat expr
> while (condition) expr
> name <- function(arg_1, arg_2, ...)
expression
> twosam <- function(y1, y2) {
> tstat <-
twosam(data" border="0" class="latex" /> male, dat > fun1 <-
function(data, data.frame, graph, limit) {
> ans <- fun1(d, df,
TRUE, 20)
> ans <- fun1(d, df, graph=TRUE, limit=20)
>
ans <- fun1(data=d, limit=20, graph=TRUE, data.frame=df)
> fun1
<- function(data, data.frame, graph=TRUE, limit=20) { ... }
> ans
<- fun1(d, df)
> ans <- fun1(d, df, limit=10)
>
bdeff <- function(blocks, varieties) {
> temp <- X
>
dimnames(temp) <- list(rep("", nrow(X)), rep("", ncol(X)))
> temp;
rm(temp)
> no.dimnames(X)
S> cube(2)
S> n <-
3
S> cube(2)
R> cube(2)
if(amount >
total)
> .First <- function() {
> .Last <-
function() {
> methods(class="data.frame")
>
methods(plot)
> coef
> methods(coef)
>
getAnywhere("coef.aov")
> getS3method("coef", "aov")
>
fitted.model <- lm(formula, data = data.frame)
> fm2 <- lm(y ~
x1 + x2, data = production)
> fm <- aov(yield ~ v + n*p*k +
Error(farms/blocks), data=farm.data)
> anova(fitted.model.1,
fitted.model.2, ...)
> new.model <- update(old.model,
new.formula)
> fm05 <- lm(y ~ x1 + x2 + x3 + x4 + x5, data =
production)
> fm6 <- update(fm05, . ~ . + x6)
> smf6
<- update(fm6, sqrt(.) ~ .)
> fmfull <- lm(y ~ . , data =
production)
> fitted.model <- glm(formula,
family=family.generator, data=data.frame)
> fm <- glm(y ~ x1 + x2,
family = gaussian, data = sales)
> fm <- lm(y ~ x1+x2,
data=sales)
> kalythos <- data.frame(x = c(20,35,45,55,70), n =
rep(50,5),
> kalythos" alt="female); tstat
> bslash <-
function(X, y) {
> regcoeff <- bslash(Xmat, yvar)
>
"%!%" <- function(X, y) { ... }
> fun1 <- function(data,
data.frame, graph, limit) {
> ans <- fun1(d, df, TRUE, 20)
> ans <- fun1(d, df, graph=TRUE, limit=20)
> ans <-
fun1(data=d, limit=20, graph=TRUE, data.frame=df)
> fun1 <-
function(data, data.frame, graph=TRUE, limit=20) { ... }
> ans <-
fun1(d, df)
> ans <- fun1(d, df, limit=10)
> bdeff
<- function(blocks, varieties) {
> temp <- X
>
dimnames(temp) <- list(rep("", nrow(X)), rep("", ncol(X)))
> temp;
rm(temp)
> no.dimnames(X)
S> cube(2)
S> n <-
3
S> cube(2)
R> cube(2)
if(amount >
total)
> .First <- function() {
> .Last <-
function() {
> methods(class="data.frame")
>
methods(plot)
> coef
> methods(coef)
>
getAnywhere("coef.aov")
> getS3method("coef", "aov")
>
fitted.model <- lm(formula, data = data.frame)
> fm2 <- lm(y ~
x1 + x2, data = production)
> fm <- aov(yield ~ v + n*p*k +
Error(farms/blocks), data=farm.data)
> anova(fitted.model.1,
fitted.model.2, ...)
> new.model <- update(old.model,
new.formula)
> fm05 <- lm(y ~ x1 + x2 + x3 + x4 + x5, data =
production)
> fm6 <- update(fm05, . ~ . + x6)
> smf6
<- update(fm6, sqrt(.) ~ .)
> fmfull <- lm(y ~ . , data =
production)
> fitted.model <- glm(formula,
family=family.generator, data=data.frame)
> fm <- glm(y ~ x1 + x2,
family = gaussian, data = sales)
> fm <- lm(y ~ x1+x2,
data=sales)
> kalythos <- data.frame(x = c(20,35,45,55,70), n =
rep(50,5),
> kalythos" title="female); tstat
> bslash
<- function(X, y) {
> regcoeff <- bslash(Xmat, yvar)
> "%!%" <- function(X, y) { ... }
> fun1 <- function(data,
data.frame, graph, limit) {
> ans <- fun1(d, df, TRUE, 20)
> ans <- fun1(d, df, graph=TRUE, limit=20)
> ans <-
fun1(data=d, limit=20, graph=TRUE, data.frame=df)
> fun1 <-
function(data, data.frame, graph=TRUE, limit=20) { ... }
> ans <-
fun1(d, df)
> ans <- fun1(d, df, limit=10)
> bdeff
<- function(blocks, varieties) {
> temp <- X
>
dimnames(temp) <- list(rep("", nrow(X)), rep("", ncol(X)))
> temp;
rm(temp)
> no.dimnames(X)
S> cube(2)
S> n <-
3
S> cube(2)
R> cube(2)
if(amount >
total)
> .First <- function() {
> .Last <-
function() {
> methods(class="data.frame")
>
methods(plot)
> coef
> methods(coef)
>
getAnywhere("coef.aov")
> getS3method("coef", "aov")
>
fitted.model <- lm(formula, data = data.frame)
> fm2 <- lm(y ~
x1 + x2, data = production)
> fm <- aov(yield ~ v + n*p*k +
Error(farms/blocks), data=farm.data)
> anova(fitted.model.1,
fitted.model.2, ...)
> new.model <- update(old.model,
new.formula)
> fm05 <- lm(y ~ x1 + x2 + x3 + x4 + x5, data =
production)
> fm6 <- update(fm05, . ~ . + x6)
> smf6
<- update(fm6, sqrt(.) ~ .)
> fmfull <- lm(y ~ . , data =
production)
> fitted.model <- glm(formula,
family=family.generator, data=data.frame)
> fm <- glm(y ~ x1 + x2,
family = gaussian, data = sales)
> fm <- lm(y ~ x1+x2,
data=sales)
> kalythos <- data.frame(x = c(20,35,45,55,70), n =
rep(50,5),
> kalythos" border="0" class="latex" /> Ymat <-
cbind(kalytho n - kalytho
minimum/(length(y) - 2) * solve(ou
hessian)))
> pairs(X)
> coplot(a ~ b | c)
>
coplot(a ~ b | c + d)
> plot(x, y, type="n"); text(x, y,
names)
> text(x, y, expression(paste(bgroup("(", atop(n, x), ")"),
p^x, q^{n-x})))
> help(plotmath)
> example(plotmath)
> demo(plotmath)
> help(Hershey)
> demo(Hershey)
> help(Japanese)
> demo(Japanese)
> text(locator(1),
"Outlier", adj=0)
> plot(x, y)
> identify(x, y)
> oldpar <- par(col=4, lty=2)
> par(oldpar)
> oldpar
<- par(no.readonly=TRUE)
> par(oldpar)
> plot(x, y,
pch="+")
> legend(locator(1), as.character(0:25), pch =
0:25)
> postscript()
> dev.off()
>
postscript("file.ps", horizontal=FALSE, height=5, pointsize=10)
>
postscript("plot1.eps", horizontal=FALSE, onefile=FALSE,
>
library()
> library(boot)
> search()
>
loadedNamespaces()
> help.start()
w <- ifelse(Mod(w) > 1,
1/w, w)
R [options] [<infile] [>outfile],
Note that input and
output can be redirected in the usual way (using 鈥?鈥?and 鈥?鈥?, but the line
length limit of 4095 bytes still applies. Warning and error messages are sent to
the error channel (stderr).
q(status=<exit status code>)
Many
of these use either Control or Meta characters. Control characters, such as
Control-m, are obtained by holding the <CTRL> down while you press the
<m> key, and are written as C-m below. Meta characters, such as Meta-b,
are typed by holding down <META>28 and pressing <b>, and written as
M-b in the following. If your terminal does not have a <META> key enabled,
you can still type Meta characters using two-character sequences starting with
ESC. Thus, to enter M-b, you could type <ESC><b>. The ESC character
sequences are also allowed on terminals with real Meta keys. Note that case is
significant for Meta characters.
The R program keeps a history of the command
lines you type, including the erroneous lines, and commands in your history may
be recalled, changed if necessary, and re-submitted as new commands. In
Emacs-style command-line editing any straight typing you do while in this
editing phase causes the characters to be inserted in the command you are
editing, displacing any characters to the right of the cursor. In vi mode
character insertion mode is started by M-i or M-a, characters are typed and
insertion mode is finished by typing a further <ESC>. (The default is
Emacs-style, and only that is described here: for vi mode see the readline
documentation.)
Pressing the <RET> command at any time causes the
command to be re-submitted.
<DEL>
<RET>
The final
<RET> terminates the command line editing sequence.
>: Logical
vectors
>=: Logical vectors
An introduction to r,布布扣,bubuko.com
原文:http://www.cnblogs.com/jsquare/p/3658355.html