R命令参考卡片中文版
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
;
I /O file . . file="" . (Connections) (file), (pipes), (zipped files) R windows clipboard . Excel Excel , x <- read.delim("clipboard") Excel , write.table(x,"clipboard",sep="\t",col.names=NA) clipboard , Excel , RODBC, DBI,RMySQL, RPgSQL, and ROracle . XML, hdf5, netCDF . c(...) ; recursive=TRUE
expand.grid() rbind(...) cbind(...) .
,
,
x[n] x[-n] x[1:n] x[-(1:n)] x[c(1,4,2)] x["name"] x[x > 3] x[x > 3 & x < 5] x[x %in% c("a","and","the")] x[n] x[[n]] x[["name"]] x$name x[i,j] x[i,] x[,j] x[,c(1,3)] x["name",] ( x[["name"]] x$name n n "name" . (i,j) i j 1
.
comment.char="" "#" ; skip=n n ; ,NA , 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 , save(file,...) save.image(file) load() save dump("x","...") x “...” cat(..., file="", sep=" ") ; sep print(a, ...) a format(x,...) , R write.table(x,file="",row.names= T ,col.names= T , sep="") x , ; quote TRUE, (") ; sep ; eol ; na ; col.names=NA sink(file) file, sink()
,
, .
R ‘x’
; ; as.is=TRUE header=TRUE factors;
sort(x) x ; :rev(sort(x)) cut(x,breaks) x ( ); breaks . match(x, y) x y , NA which(x == a) TRUE , x choose(n, k) =n!/[(n − k)!k!] sign(x) 0, 1 , -1 , 0 na.omit(x) NA , x , na.fail(x) x NA unique(x) x , duplicated(x) x table(x) x ( subset(x, ...) x ... select x , : x$V1 < 10
3 "name" ) "name" .
as.array(x), as.data.frame(x), as.numeric(x), as.logical(x), as.complex(x), as.character(x), , ; : methods(as) is.na(x), is.null(x), is.array(x), is.data.frame(x), is.numeric(x), is.complex(x), is.character(x), ... ; , methods(is) length(x) x dim(x) ; dim(x) <- c(3,2) dimnames(x) nrow(x) NROW(x) dim(x)[1] ncol(x) NCOL(x) dim(x)[2] class(x) x ;class(x) <- "myclass" unclass(x) x names(x) (names) unname(x) R (names) (dimnames) unlist(x) x attr(x,which) x which attributes(obj) obj which.max(x) which.min(x) rev(x) x rle(x) x x Runs
n n n n+1 "name" 3 (3,5) x
(来自百度文库
)
”topic”
help.start() HTML demo() R example(f) str(a) R (*str*ucture) summary(a) a , a . ls() “ ” ; ls.str() str() dir() list.files() getwd() setwd() methods(a) a “S3 methods” methods(class=class(a)) options(...) ; error install.packages(pkg) pkg update.packages() library(pkg) pkg require(x) library(help=pkg) pkg attach(x) x R ;x save R data file. search() detach(x) attach . assign(x,value) value x, ”< −” quit() R (q() Ctrl z) data(x) edit() fix(x) ‘fix’ ‘edit’ data.entry(x) scan(x) read.table(file) sep="" ;
), ;
,
sample(x, size) replace = TRUE prop.table(x,margin=) margin ,
x margin 1
size
, ,
pmin(x,y,...) x[i], y[i] , pmax(x,y,...) . cumsum(x) x , x[i]=sum{ x[1]: x[i]} cumprod(x) . cummin(x) . cummax(x) . union(x,y) x ∪ y − x ∩ y intersect(x,y) x ∩ y setdiff(x,y) x − x ∩ y setequal(x,y) x, y ( x, y is.element(el,set) x %in% y Re(x) Im(x) Mod(x) ( ); abs(x) Arg(x) (in radians) Conj(x) x convolve(x,y) fft(x) (array) mvfft(x) filter(x,filter) na.rm=FALSE t(x) diag(x) %*% solve(a,b) a %*% x = b x solve(a) eigen(x) rowsum(x) ; rowSums(x) colsum(x), colSums(x) . rowMeans(x) colMeans(x) dist(x) x apply(X,INDEX,FUN=) , lapply(X,FUN) FUN tapply(X,INDEX,FUN=) FUN sapply lapply, by(data,INDEX,FUN) merge(a,b) xtabs(a b,data=x) aggregate(x,by,FUN) , stack(x, ...) unstack(x, ...) stack() reshape(x, ...) ‘wide’ ‘wide’ (direction=“long”) (INDEX) X x (INDEX) FUN
R
( com , , sunbjt@gmail.com. 1.3 2008-8-3 ) Tom Short tshort@eprisolutions. www.Rpad.org . ( Tom Short ) . ,
R help(topic) topic ?topic help.search("topic") apropos("topic")
expression(expr) ‘ ’(expression) is.expression(x), as.expression(x, ...) parse(file = "", n = NULL) , (expression) eval(expr) R (expression)
.
).
(NA).
paste(...) ; sep= ( ); collapse= “collapsed” substr(x,start,stop) ; , substr(x, start, stop) <- value strsplit(x,split) split x grep(pattern,x) pattern ; ?regex gsub(pattern,replacement,x) , sub() , tolower(x) toupper(x) casefold(x, upper = TRUE) x (TRUE) (FALSE) chartr(old, new, x) x old new match(x,table) table x . x %in% table . pmatch(x,table) table x nchar(x) . POSIXct . ( .>), seq() difftime() . Date + ?DateTimeClasses . chron . as.Date(s) as.POSIXct(s) ; format(dt) . “2006-07-24”. . : Date %a, %A “ ”(weekday) %b, %B %d (01–31). %H (00–23). %I (01–12). %j (001–366). %m (01–12). %M (00–59). %p AM/PM . %S (00–61). %U (00–53); %w (0–6, 0). %W (00–53); %y (00–99). . %Y . %z ( .) ; -0800 %Z ( .) ( ). weekdays(x) months(x) quarters(x) . x x x (Q1 - Q4) , “ ”
+,-,×,÷,ˆ,%%,%/% < > <= >= ==..!=.. sin,cos,tan,asin,acos,atan,atan2,log,log10,exp max(x) x min(x) . range(x) c(min(x), max(x)) sum(x) x diff(x) x prod(x) x mean(x) x abs(x) x sqrt(x) x0.5 median(x) x quantile(x,probs=) , 0,0.25,0.75,1 IQR(x) 50% weighted.mean(x, w) rank(x) x var(x) or cov(x) x ; x , sd(x) x ; sd(x)=sqrt(var(x)) cor(x) x , var(x, y) or cov(x, y) x y , x y cor(x, y) x y ; n base , center=FALSE, center=TRUE, scale=TRUE) integrate(f,lower,upper) f ) x round(x, n) x log(x, base) scale(x) x ,
: width, digits, . from:to ; “:” ; 1:4 + 1 “2,3,4,5” seq(from,to) by= ; length= seq(along=x) 1, 2, ..., length(along); rep(x,times) x times ; each= x ;rep(c(1,2,3),2) 1 2 3 1 2 3; rep(c(1,2,3),each=2) 112233 data.frame(...) , ; data.frame(v=1:4,ch=c("a","B","c","d"),n=10); list(...) , list(a=c(1,2),b="hi",c=3i); array(x,dim=) x ; ; x , x matrix(x,nrow=,ncol=) ; factor(x,levels=) x gl(n,k,length=n*k,labels=1:n) ( ); k ;n ; dim=c(3,4,2)