Data Object Management
The following functions are useful for data object management in R:
Function | Description |
---|---|
class() | Identify the class of a named object. |
colnames(); rownames() | Retrieve or set the column or row names of an object. |
dim() | Retrieve or set the dimensions of a rectangular data object. |
dimnames() | Get or set the dim names of an object. |
head() | Returns the first n rows of a data object. |
grep() | search for data objects or files based on a pattern within each element of a character vector. See chapter "Misc Tricks" for a full discussion on wind cards and regular expressions. |
length() | Get or set the length of vectors (including lists) and factors. |
ls() | returns a vector of character strings giving the names of the objects inn the specified environment (frame or directory). If no environment is specified, ls() shows what data and functions a user has defined. |
ls.str() | A variation of ls() that also provides data object structure information. |
lsf.str() | A variation of ls() that also provides information on function objects only. |
mode(0 | set or get the storage mode of a data object. |
nchar() | Count the number of characters in a character vector. |
ncol(); nrow() | return the number of columns or rows of a data object. |
objects() | same as ls(). |
rm() remove() | remove objects from memory. Can be used with pattern recognition |
str() | Display the internal structure of an R object; a diagnostic function that is very helpful assessing nested lists, geo-spatial objects with several layers and other large data objects with meta data. |
summary() | Result summary for describing model objects; results depend on data class. |
tail() | Returns the last n rows of a data object. |
Simple Tricks to Manage Objects in Memory
For example, some handy data management commands appear below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# remove all objects in the global environment rm(list=ls()) # remove all objects except object a rm(list = ls()[-a] # remove only function objects rm(list=lsf.str()) # remove data objects, but preserve function objects rm(list = setdiff(ls(), lsf.str())) # remove everything, but keep objects a and b ls()[!(ls() %in% c("a","b"))] # remove all objects with temp in the name rm(list=ls(pattern="temp")) # remove all objects starting with abc rm(ls(pattern="^abc")) |