DOC PREVIEW
Berkeley STAT 135 - Short - reference card

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

R Reference Cardby Tom Short, EPRI PEAC, [email protected] 2004-11-07Granted to the public domain. See www.Rpad.org for the source and latestversion. Includes material from R for Beginners by Emmanuel Paradis (withpermission).Getting helpMost R functions have online documentation.help(topic) documentation on topic?topic id.help.search("topic") search the help systemapropos("topic") the names of all objects in the search list matchingthe regular expression ”topic”help.start() start the HTML version of helpstr(a) display the internal *str*ucture of an R objectsummary(a) gives a “summary” of a, usually a statistical summary but it isgeneric meaning it has different operations for different classes of als() show objects in the search path; specify pat="pat" to search on apatternls.str() str() for each variable in the search pathdir() show files in the current directorymethods(a) shows S3 methods of amethods(class=class(a)) lists all the methods to handle objects ofclass aInput and outputload() load the datasets written with savedata(x) loads specified data setslibrary(x) load add-on packagesread.table(file) reads a file in table format and creates a dataframe from it; the default separator sep="" is any whitespace; useheader=TRUE to read the first line as a header of column names; useas.is=TRUE to prevent character vectors from being converted to fac-tors; use comment.char="" to prevent "#" from being interpreted asa comment; use skip=n to skip n lines before reading data; see thehelp for options on row naming, NA treatment, and othersread.csv("filename",header=TRUE) id. but with defaults set forreading comma-delimited filesread.delim("filename",header=TRUE) id. but with defaults setfor reading tab-delimited filesread.fwf(file,widths,header=FALSE,sep="!",as.is=FALSE)read a table of f ixed width f ormatted data into a ’data.frame’; widthsis an integer vector, giving the widths of the fixed-width fieldssave(file,...) saves the specified objects (...) in the XDR platform-independent binary formatsave.image(file) saves all objectscat(..., file="", sep=" ") prints the arguments after coercing tocharacter; sep is the character separator between argumentsprint(a, ...) prints its arguments; generic, meaning it can have differ-ent methods for different objectsformat(x,...) format an R object for pretty printingwrite.table(x,file="",row.names=TRUE,col.names=TRUE,sep=" ") prints x after converting to a data frame; if quote is TRUE,character or factor columns are surrounded by quotes ("); sep is thefield separator; eol is the end-of-line separator; na is the string formissing values; use col.names=NA to add a blank column header toget the column headers aligned correctly for spreadsheet inputsink(file) output to file, until sink()Most of the I/O functions have a file argument. This can often be a charac-ter string naming a file or a connection. file="" means the standard input oroutput. Connections can include files, pipes, zipped files, and R variables.On windows, the file connection can also be used with description ="clipboard". To read a table copied from Excel, usex <- read.delim("clipboard")To write a table to the clipboard for Excel, usewrite.table(x,"clipboard",sep="\t",col.names=NA)For database interaction, see packages RODBC, DBI, RMySQL, RPgSQL, andROracle. See packages XML, hdf5, netCDF for reading other file formats.Data creationc(...) generic function to combine arguments with the default forming avector; with recursive=TRUE descends through lists combining allelements into one vectorfrom:to generates a sequence; “:” has operator priority; 1:4 + 1 is “2,3,4,5”seq(from,to) generates a sequence by= specifies increment; length=specifies desired lengthseq(along=x) generates 1, 2, ..., length(along); useful for forloopsrep(x,times) replicate x times; use each= to repeat “each” el-ement of x each times; rep(c(1,2,3),2) is 1 2 3 1 2 3;rep(c(1,2,3),each=2) is 1 1 2 2 3 3data.frame(...) create a data frame of the named or unnamedarguments; data.frame(v=1:4,ch=c("a","B","c","d"),n=10);shorter vectors are recycled to the length of the longestlist(...) create a list of the named or unnamed arguments;list(a=c(1,2),b="hi",c=3i);array(x,dim=) array with data x; specify dimensions likedim=c(3,4,2); elements of x recycle if x is not long enoughmatrix(x,nrow=,ncol=) matrix; elements of x recyclefactor(x,levels=) encodes a vector x as a factorgl(n,k,length=n*k,labels=1:n) generate levels (factors) by spec-ifying the pattern of their levels; k is the number of levels, and n isthe number of replicationsexpand.grid() a data frame from all combinations of the supplied vec-tors or factorsrbind(...) combine arguments by rows for matrices, data frames, andotherscbind(...) id. by columnsSlicing and extracting dataIndexing vectorsx[n] nthelementx[-n] all but the nthelementx[1:n] first n elementsx[-(1:n)] elements from n+1 to the endx[c(1,4,2)] specific elementsx["name"] element named "name"x[x > 3] all elements greater than 3x[x > 3 & x < 5] all elements between 3 and 5x[x %in% c("a","and","the")] elements in the given setIndexing listsx[n] list with elements nx[[n]] nthelement of the listx[["name"]] element of the list named "name"x$name id.Indexing matricesx[i,j] element at row i, column jx[i,] row ix[,j] column jx[,c(1,3)] columns 1 and 3x["name",] row named "name"Indexing data frames (matrix indexing plus the following)x[["name"]] column named "name"x$name id.Variable conversionas.array(x), as.data.frame(x), as.numeric(x),as.logical(x), as.complex(x), as.character(x),... convert type; for a complete list, use methods(as)Variable informationis.na(x), is.null(x), is.array(x), is.data.frame(x),is.numeric(x), is.complex(x), is.character(x),... test for type; for a complete list, use methods(is)length(x) number of elements in xdim(x) Retrieve or set the dimension of an object; dim(x) <- c(3,2)dimnames(x) Retrieve or set the dimension names of an objectnrow(x) number of rows; NROW(x) is the same but treats a vector as a one-row matrixncol(x) and NCOL(x) id. for columnsclass(x) get or set the class of x; class(x) <- "myclass"unclass(x) remove the class attribute of xattr(x,which) get or set the attribute which of xattributes(obj) get or set the list of attributes of objData selection and manipulationwhich.max(x) returns the index of the greatest element of xwhich.min(x) returns the index of the smallest element of xrev(x) reverses the elements of xsort(x) sorts the elements of x in


View Full Document

Berkeley STAT 135 - Short - reference card

Download Short - reference card
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Short - reference card and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Short - reference card 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?