R ëëëkkk¡¡¡
by Tom Short, EPRI PEAC, tshort@epri-peac.com 2004-10-21
translated by SZ Liu, sunbjt@hotmail.com 2006-09-22
Granted to the public domain. See www.Rpad.org for the source and latest
version. Includes material from R for Beginners by Emmanuel Paradis (with
permission).
version 1.0 2006-09-22
¼¼¼ÏÏÏ
Ü©R ¼êÑk3©"help(topic) 'utopic©.
?topic Óþ
help.search("topic") |¢ÏXÚ
apropos("topic") £¤k3|¢´»e÷vKLª”topic”
¤k鶡
help.start() HTML/ªÏ
str(a) w«R éS3á5(*str*ucture)½{`²é
summary(a) ÑaV, Ï~´5ÚOV;§kØÓ
ÅnÚaá5
ls() w«|¢´»eé; ½pat="pat",Uª^|¢
ls.str() str() |¢´»ezCþ
dir() 3c8¹ew«©
methods(a) w«a“S3 methods”
methods(class=class(a)) L¤k±)ûáuéa{
ÑÑÑ\\\ÑÑÑÑÑÑ
load() \1dsave·-ê|
data(x) \1½ê|
library(x) \1÷
read.table(file) Ö L ª © ¿ ò Ù M ï ¤ ê â µ;%
@©Îsep=""?¿whitespace;¦^header=TRUE Ö1
1 I K;¦ ^as.is=TRUE i Î þ C factors;¦
^comment.char="""#")º5º; ¦^skip=n 3Öê
âcaLn 1;[Ï'u1·¶,NA ?n,ÚÙ¦
read.csv("filename",header=TRUE) Óþ,%@ÖÏ
:©©
read.delim("filename",header=TRUE) Ó þ,% @ Ö
tab ©©
read.fwf(file,widths,header=FALSE,sep="
",as.is=FALSE)
±f ixed width f ormatted/ªÖêâêâµ; widths ´ê
þ, ^uN°Ýiã
save(file,...) ±Ø©²?½é
save.image(file) ¤ké
cat(..., file="", sep=" ") r=ziÎ<arguments;
sep argumentsm©iÎ
print(a, ...) w«arguments; 5, §kØÓÅnÚaá
5
format(x,...) ªz,Ðw«R é
write.table(x,file="",row.names=TRUE,col.names=TRUE,
sep=" ") 3rx=zê âµ,©; XJquote TRUE,
iÎÚÏfÒ¬(")¤; sep ´iã©Î; eol 1
©Î; na "iÎG; ¦^col.names=NA O\IK±
BuÚLÑ\
sink(file) ÑÑ©file, Ñ\·-sink()
Ü © I /O ¼ ê Ñ kfile ë ê.§ ± ´ ½ ¶ ¡ ½ ë i Î
G. file="" ¿ X I O Ñ \ ½ Ñ Ñ. ë(Connections) ± º ©
(file),+(pipes),Ø©(zipped files)½ R Cþ.
3 windows ö¸e.©¦^±ÏLi(clipboard)
ª.Ö Excel L,±ò Excel ¥êâi,¦^
x <- read.delim("clipboard")ªÖêâ.XJòêâ\
iø Excel ¦^,±¦^
write.table(x,"clipboard",sep="\t",col.names=NA)
ê⥡pA^,RODBC, DBI,RMySQL, RPgSQL, and ROracle÷
. ÖÙ¦©ªëXML, hdf5, netCDF ÷.
êêêâââMMMïïï
c(...) ~òXëê=zþ¼ê; ÏLrecursive=TRUE
üSüL¿|ܤkþ.
from:to )S; “:” kp?O`k?; 1:4 + 1 “2,3,4,5”
seq(from,to) )Sby= ½OÌ; length= ½¦Ý
seq(along=x) generates 1, 2, ..., length(along); useful for for
loops
rep(x,times) Ex timesg; ¦ ^each= 5 Ex each g;
rep(c(1,2,3),2) 1 2 3 1 2 3; rep(c(1,2,3),each=2) 1
1 2 2 3 3
data.frame(...) M ï ê â µ,C þ U · ¶ ½ · ¶;
data.frame(v=1:4,ch=c("a","B","c","d"),n=10); é á
þ¬W¿þÝ
list(...) MïdCþ|¤ L,CþU ·¶½ ·¶;
list(a=c(1,2),b="hi",c=3i);
array(x,dim=) )dx|¤ê|;¦^aqdim=c(3,4,2)½
ê; XJxØÝ,KxgÄÌ
matrix(x,nrow=,ncol=) Ý;Óþ
factor(x,levels=) rþx ?è¤Ïf.
gl(n,k,length=n
*
k,labels=1:n) Ï L ½ Y ² ª ) Y
²(Ïf); k Y²ê; n Egê
expand.grid() þÚÏf|ܤêâµ
rbind(...) r±1/ª|ÜÝ,êâµ,½Ù¦
cbind(...) Óþ.±/ª
êêêâââÚÚÚ©©©lll
þI
x[n] 1n
x[-n] Ø1nx
x[1:n] cn
x[-(1:n)] 1n+1
x[c(1,4,2)] ½
x["name"] ¶"name"
x[x > 3] ¤ku3
x[x > 3 & x < 5] «m(3,5)
x[x %in% c("a","and","the")] ½|¥
LI
x[n] Lw«n
x[[n]] L1n
x[["name"]] ¶"name"
x$name Óþ.
ÝI
x[i,j] eI(i,j)
x[i,] 1i1
x[,j] 1j
x[,c(1,3)] 11Ú3
x["name",] ¶"name"1
êâµI(ÝI\eã)
x[["name"]] ¶"name"
x$name Óþ.
CCCþþþCCC
as.array(x), as.data.frame(x), as.numeric(x),
as.logical(x), as.complex(x), as.character(x),
... =Cþa.; ¦^Xe·-ÜL, methods(as)
CCCþþþ&&&EEE
is.na(x), is.null(x), is.array(x), is.data.frame(x),
is.numeric(x), is.complex(x), is.character(x),
... uCþa.; ¦^Xe·-ÜL, methods(is)
length(x) x¥ê
dim(x) #½éê; dim(x) <- c(3,2)
dimnames(x) #½é¶¡
nrow(x) 1ê; NROW(x) ^ Ó,´§rþwü1
Ý
ncol(x) ÚNCOL(x) Óþ.
class(x) ½xa;class(x) <- "myclass"
unclass(x) íØxa
attr(x,which) ½xá5a.which
attributes(obj) ½obj á5L
êêêâââÀÀÀÚÚÚööö
which.max(x) £x¥I
which.min(x) £x¥I
rev(x) =x¥¤k
sort(x) ,Süx¥;üSü¦^:rev(sort(x))
cut(x,breaks) òx©¤Aã(½Ïf);breaksã꽩:
þ.
match(x, y) £ÚxÓÝÚy¥þØK
£NA
which(x == a) XJ'öý(TRUE),£þx.
choose(n, k) |ÜêO
na.omit(x) Ø"(NA)(Ø'1XJx ݽêâµ)
na.fail(x) £Ø&EXJx¹NA
unique(x) XJxþ½êâµ,£
table(x) £dx ØÓê|¤L(~^uê½Ïf),=
ªêL
subset(x, ...) â ^ (...À x¥ ,X:x$V1 < 10); X
Jxêâµ,Àselect¦^KÒÑ3½ØCþ
sample(x, size) Ø £ Å 3 þx¥ Ä size ,À
replace = TRUE#N£Ä
prop.table(x,margin=) âmargin ¦^©êL«L,Ãmargin
,¤kÚ1
êêêÆÆÆ
+,-,×,÷,ˆ
sin,cos,tan,asin,acos,atan,atan2,log,log10,exp