R‎ > ‎

R Functions

This is a list of R functions that I commonly use, along with a few that are very useful, but easy to forget.
Showing 40 items
FunctionDescription
Sort 
 
Sort 
 
FunctionDescription
all(x) are all the logicals (i.e. x) true? 
anova(x,y) Tests is the regressions x or y are significantly different from one another. 
any(x) are any of the logicals (i.e. x) true? 
assign(x,value,...) I looked for months for this function, allows the use of paste() to create variables 
attach.all() Like attach, but it overwrites the data - has a dialogue box to prompt for overwriting. In the arm package. See Custom functions for the code. 
axis(side,...) Adds an axis to a plot, you can specify your own scale, which is nice when you don't want R to decide what labels to print. 
by() Very useful function that applies a function to certain levels of a factor. 
class() Returns the class of an object or variable. 
colSums(x); rowSums(); colMeans(); rowMeans() Sum by column and rows. apply(x,2,sum) does sums, too. 
contrasts() <- contr.treatment() Specifies the number of contrasts to make for a variable. After this is run, the variables can be put in a regression model. The contr.treatment specifies dummy coding for each category. 
contrasts(age.f) <- contr.treatment(4, base = 2) Re-specifies the reference categories, in the above case, 2 is the reference category and 4 is the number of categories. 
CrossTable() In the package "gmodels." Provides very nice crosstabs in R, with a nice, fixed-width format for the output. Can calculate proportions by row and/or columns. Very useful function. 
C(x,base=1) Sets the contrasts attribute associated with a factor - especially useful for turning a vector into a factor, with custom factor levels: this is a handy way to respecify the base category in a regression model. 
get("characterstring") Use this function to get a variable named with this character string. This is useful for passing a character string, which is the name of a variable, into a dataset and then attaching a dataframe and using get() to get the values of the variable with the name specified. 
ifelse(conditional,valtrue,valfalse) A short form of an if() else loop - very handy 
ifelse(condition,T,F) Condensed if-else statement, where T F are the return values. Can be nested. 
install.packages("NAME",dependencies=TRUE,repos="http://cran.cnr.berkeley.edu/") Download a package and install with dependencies. 
jitter() Add numbers around data points to expand a plot. 
lapply(data.frame,function) Applies a function to all the elements in a data frame. 
latex() A very flexible method of generating latex tables. Part of the Hmisc package. LOTS of customizations. 
lm(write ~ I(age4 == 1) + I(age == 3) + I(age4 == 4)) Another way of specifying the base category in regression models in R (2 is the base in this example) 
ok= !is.na (x+y+z); clean=as.data.frame(cbind(x,y,z)[ok,]) Creates an index of listwise deletion, you can filter the data on the ok index to remove the listwise cases. 
paste(x,y,...) Concatenates x and y, etc. as characters. Can be useful for generating patterns. 
pR2 Pseudo R2 for logits, etc, in pscl package 
recode() {car package} Recodes a numeric vector, character vector, or factor according to simple recode specifications.  
relevel(x$y,ref="NAME")  Reorders a variable to make "NAME" the reference level 
rm(list=ls()) Removes all objects from the environment 
sample() Generate random integers 
score.items() in library(psych) Cronbach's alphas, variance and reliability in scales 
source() Reads in an R script from another file. 
split() Splits a data frame into vectors 
split(x,f) splits x into the categories defined by f, if you want only one category of f then use split(x,f)$CAT 
str() Structure of the object. This is a very helpful function that lets you figure out how an object is structured. 
toupper(); tolower() To upper case; to lower case. 
unclass() Removes the class attribute from an object. Very handy when you're trying to print the results of a regression. 
unique() This returns the unique values of a vector. Very useful for dumping duplicate cases. 
x[-c(1,2,3),] Remove multiple rows (or columns). 
x[,drop=TRUE] Drops the unused levels of a factor. factor(x) also works. 
x%in%y tests each element of x for membership in y 
xtable() A quick method to generate latex tables. Not nearly as flexible as Hmisc:latex, but easier. Part of the xtable package. 
Showing 40 items