Short List of Important R Commands

  • head: show the first k elements of a dataset
head(agesex, 3)
##           Age  Male Female
## 1 Less than 1 29601  28442
## 2           1 29543  28130
## 3           2 30252  28881
  • ls: list of all elements of the RData file
head(ls())
## [1] "acorn"        "agesex"       "agesexUS"     "aids"        
## [5] "airfilters"   "airpollution"
  • attach: make column names of a data frame usable
table(Gender)
## [1] "Error: object Gender not found"
attach(upr)
head(Gender)
## [1] "M" "M" "M" "F" "F" "F"
  • args: show arguments of a routine
args(stat.table)
## function (y, x, Mean = TRUE, Sort = FALSE, ndigit = 1) 
## NULL

this does not always work:

args(mean)
## function (x, ...) 
## NULL

in that case use

  • ?: show details of routine
?mean

will open a help file in a browser.

  • length: number of elements of a vector

  • dim: number of row and columns of a data frame

  • colnames: names of columns of a data frame

  • rownames: names of rows of a data frame

  • sum, mean, sd

  • table: count the number of occurances

table(Gender)
## Gender
##     F     M 
## 11487 12179
table(Gender, Year)
##       Year
## Gender 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
##      F 1102 1040 1162 1137 1208 1219 1180  958  853  769  859
##      M 1151 1118 1138 1098 1256 1219 1237 1073  919  979  991