R ëëë•••kkk¡¡¡
© (=©) d Tom Short tshort@eprisolutions.
com >§\Œ±3 www.Rpad.org þ•#©.
¥©‡© (®¼ Tom Short €ÈNŒ) (
þÓ‡aq§ÛÜV\eZ~^·-"Y?
¾±9‘od 4g)KI§Xkïƽ¦¯žéX
e‡ sunbjt@gmail.com ½Æ•Ì• http://bjt.name
¥©‡µ2008 c 8 3 F?¾
•••ÏÏÏÚÚÚÄÄÄ:::
ŒÜ© R ¼êÑk3‚©"
help(topic) 'u topic ©.
?topic Óþ
help.search("topic") |¢•ÏXÚ
apropos("topic") ˆ£3|¢´»e•¹ (Ü©) '…c”topic”
¤ké–¶¡
help.start() HTML /ª•Ï
demo() R õUü«
example(f) $13‚•Ï¥~f
str(a) w« R é–S3á5 (*str*ucture) ½{‡`²é–
summary(a) ‰Ñ a V ‡, Ï~´˜‡˜„5ÚOV‡; …§éØÓ
á5 a kØÓöŠ•ª.
ls() w« “|¢´»” eé–; •ŒU•½^‡|¢"
ls.str() str() |¢´»ez‡Cþ†Ùá5
dir() w«c8¹e©‡
list.files() Óþ
getwd() ¼óŠ´»&E
setwd() ˜óŠ´»
methods(a) w« a “S3 methods”
methods(class=class(a)) L¤kŒ±)ûáué–a•{
options(...) ˜½uÛëê; ~^ëêk: width, digits,
error
install.packages(pkg) SC pkg •
update.packages() gÄé'•‡§¿Î¯•#
library(pkg) \1 pkg •
require(x) Óþ
library(help=pkg) Ы• pkg &E
attach(x) ò x •• R |¢´»; x Œ±¦˜‡L, êâµ, ½ö
´d save Mï R data file. ¦^ search() 5w«|¢´».
detach(x) attach _L§.
assign(x,value) ò value DŠ‰ x, =”< −”
quit() òÑc R ¬{ (q() ½ Ctrl z)
ÑÑÑ\\\†††ÑÑÑÑÑÑ
load() \1d save ·-]8
data(x) \1•½êâ
edit() N^©?6ì?U R é–
fix(x) ‘fix’ N^ ‘edit’ ?U ‘x’
data.entry(x) >fêâL/ª¹\?6ì
scan(x) l››½©‡¥Öêâ••þ½L
read.table(file) ÖL‚ª©‡¿òÙMï¤êâµ; %@
© • Î sep="" •? ¿ ˜ x; ¦ ^ header=TRUE Ö 1˜1
Š• IK; ¦^ as.is=TRUE “ ŽiΕþC• factors; ¦
^ comment.char="" “Ž "#" )º•5º; ¦^ skip=n 3
ÖêâcaL n 1; •[„•Ï'u1·¶,NA ?n, ÚÙ¦
read.csv("filename",header=TRUE) Ó þ, % @ ˜ • Ö
csv ©‡ (Comma Separated values)
read.delim("filename",header=TRUE) Ó þ, % @ ˜ • Ö
tab ©•©‡
read.fwf(file,widths,header=F,sep="\t",as.is=FALSE)
± f ixed width f ormatted /ªÖêâ–êâµ; widths ´ê
•þ, ^u˜N°Ýiã
save(file,...) ±Ø©²?›••½é–
save.image(file) •¤ké–
dump("x","...") òé– x •3 “...” p
cat(..., file="", sep=" ") r›=z•i΋<é–D
Š; sep •é–DŠm©•ÎÒ
print(a, ...) w« a DŠ§•˜„§§éuØÓé–Œ±
kØÓLˆ•ª"
format(x,...) ‚ªz, •Ðw« R é–
write.table(x,file="",row.names= T ,col.names= T ,
sep="") 3 r x = z • ê â µ , © ‡; X J quote
• TRUE, iÎÚÏfÒ¬ (") ¤•Œ; sep ´iã©…Î;
eol •—1©•Î; na •"”ŠiÎG; ¦^ col.names=NA O
\IK±BuÚL‚Ñ\˜—
sink(file) ÑÑ©‡ file, †Ñ\·- sink()
ŒÜ© I/O ¼êÑk file ëþ. §²~^˜‡iÎG5·¶©‡½
ë. file="" ¿›XIOÑ\½ÑÑ. ë (Connections) Œ±•¹©
‡ (file), + (pipes), Ø ©‡ (zipped files) ½ R Cþ
3 windows öŠ‚¸e§êâ•Œ±ÏLi†£clipboard¤•
ª. Ö Excel LµŒ±ò Excel ¥ê‖i†£S•¤, ¦^
x <- read.delim("clipboard") •ªÖêâ"XJ‡òêâø Ex-
cel ¦^, write.table(x,"clipboard",sep="\t",col.names=NA) Œ±
òêâ\i†£clipboard¤, Êb Excel =Œ"
ê⥕¡pA^, ž„ RODBC, DBI,RMySQL, RPgSQL, and ROracle
•. ÖÙ¦©‡‚ªë• XML, hdf5, netCDF •.
êêêâââMMMïïï
c(...) ~„ò˜Xëþ=z••þ¼ê; ÏL recursive=TRUE
üSüL¿|ܤkƒ••þ.
from:to )˜‡S; “:” kp?O`k?; 1:4 + 1 “2,3,4,5”
seq(from,to) )˜‡S by= •½må; length= •½‡¦•Ý
seq(along=x) ) 1, 2, ..., length(along); ~^3Ì‚þ
rep(x,times) -E x times g; ¦^ each= 5•½ƒ x -Eg
ê;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=) )d x |¤ê|; ¦^aq dim=c(3,4,2) •½
‘ê; XJ x Ø•Ý, K x gÄÌ‚
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() Jø•þ½Ïf¤k|ܤêâµ
rbind(...) r±1/ª|ÜÝ, êâµ, ½Ù¦
cbind(...) Óþ. ±/ª
êêêâââ©©©•••ÚÚÚÀÀÀ
•þ¢Ú
x[n] 1 n ‡ƒ
x[-n] Ø1 n ‡ƒ x
x[1:n] c n ‡ƒ
x[-(1:n)] 1 n+1 –•ƒ
x[c(1,4,2)] •½ƒ
x["name"] ¶• "name" ƒ
x[x > 3] ¤kŒu 3 ƒ
x[x > 3 & x < 5] «m (3,5) ƒ
x[x %in% c("a","and","the")] ‰½|¥ƒ
L¢Ú
x[n] Lw«ƒ n
x[[n]] L1 n ‡ƒ
x[["name"]] ¶• "name" ƒ
x$name Óþ.
Ý¢Ú
x[i,j] eI• (i,j) ƒ
x[i,] 1 i 1
x[,j] 1 j
x[,c(1,3)] 1 1 Ú 3
x["name",] ¶• "name" 1
êâµ¢Ú (Ý¢Ú\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) Ú NROW(x) ˆ£1‡ê dim(x)[1]
ncol(x) Ú NCOL(x) ˆ£‡ê dim(x)[2]
class(x) ½˜ x a;class(x) <- "myclass"
unclass(x) íØ x a
names(x) w½˜é–¶¡ (names)
unname(x) íØ R é–¶¡ (names) ½‘¶¡ (dimnames)
unlist(x) òL x =z••þ
attr(x,which) ½˜ x á5a. which
attributes(obj) ½˜ obj á5L
êêêâââÀÀÀJJJÚÚÚöööŠŠŠ
which.max(x) ˆ£ x ¥•Œƒ¢Ú
which.min(x) ˆ£ x ¥•ƒ¢Ú
rev(x) 6 x ¥¤kƒ
rle(x) ˆ£i§£Runs¤&E
sort(x) ,Sü x ¥ƒ; üSü¦^:rev(sort(x))