2. Managing objects


Here are a few functions which are useful to handle and manage objects.

ls() lists all the vectors and variables currently stored in memory.

[code language=”r”]
ls()
[/code]

list

Note that the function objects() does exactly the same.

 

rm() erases from the memory the vector or variable which is named between the brackets. In the example below, rm(text.object) removes the object or vector called text.object from the memory (providing it exists; if not, a warning message tells you the object is not to be found), as revealed by the second instance of the command ls(). Note that this action cannot be undone, so you must be sure of what you do here.

[code language=”r”]
ls()
rm(text.object)
ls()
[/code]

rm()

 

rm(list = ls()) erases (almost*) all the objects which can be listed by ls(). And there is no way back!!!

[code language=”r”]
ls()
rm(list = ls())
ls()
[/code]

rm(list = ls())

* actually this command erases all the objects that you have created, as shown in the example above. However, objects which have been “attached” may not be erased…

 

class() indicates what your object is “made of” or which type it belongs to. The object women is a dataframe prestored in R. Typing class(women) returns data.frame. It will also define whether a vector is made of integers, text or logical values:

[code language=”r”]
women
class(women)

int <- 1:10
class(int)

text <- c("stuff", "thing")
class(text)

boolean <- c(TRUE, FALSE)
class(boolean)

mtrx <- matrix(1:9, nrow=9)
class(mtrx)
[/code]

class()

 

 

  Fant du det du lette etter? Did you find this helpful?
[Average: 0]