{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture 03 - 102A_3_ho

# Lecture 03 - 102A_3_ho - Objects UCLA Department of...

This preview shows pages 1–4. Sign up to view the full content.

Jan de Leeuw October 10, 2010 Jan de Leeuw 102A_1 UCLA Department of Statistics Objects What you create or load into R all becomes an R object. There are R objects of many different types. All objects have a type and a storage mode which can be determined by typeof () and mode () . We will be mostly interested in types. Storage modes are mostly a grouping of types, for example types "float" and "integer" both have storage mode "numeric". Lets first look at a list of the important types (from the user’s point of view). Jan de Leeuw 102A_1 UCLA Department of Statistics – logical (TRUE and FALSE) – integer – double – complex – character – raw (bits) – list – NULL (the unique NULL object) – closure (a function) – environment – connection – namespace – symbol (name) Jan de Leeuw 102A_1 UCLA Department of Statistics Assignments 1 <- 2 < <- 3 assign() 4 = Jan de Leeuw 102A_1 UCLA Department of Statistics

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Scoping Jan de Leeuw 102A_1 UCLA Department of Statistics Finding Objects 1 get () 2 mget() 3 getAnywhere() Jan de Leeuw 102A_1 UCLA Department of Statistics 1 > a <- function () x <- 1 2 > a() 3 > x 4 Error: object "x" not found 5 > a <- function (x) x< <- 1 6 > a() 7 > x 8 [1] 1 Jan de Leeuw 102A_1 UCLA Department of Statistics We will say more about the various types as we encounter them later on. For the time being, we mention that all objects "foo" have a length (foo) . For some objects, such as functions, length is always one. But objects with atomic mode (logical, integer, numeric, complex, character, and raw) are usually (but not always) vectors, and have a length equal to the number of elements. R does not have a scaler type, the number 1 is really an integer vector of length one and TRUE is a logical vector of length one. Jan de Leeuw 102A_1 UCLA Department of Statistics
We have mentioned typeof () . For any type "foo", we also have the functions is .foo () and as .foo () . is .foo (bar) returns TRUE if "bar" is a "foo", and FALSE otherwise. as .foo (bar) tries to convert "bar" to type "foo". It should be emphasized that R also has various derived structures such as vectors, factors, arrays, matrices, and data.frames, which are pieced together from components of various types. They usually also have an is .foo () and as .foo () . Jan de Leeuw 102A_1 UCLA Department of Statistics Next, objects have attributes. To see the attributes of object "foo", use attributes (foo) . To find the value of attribute "bar" of "foo", use attr (foo, "bar" ) and to change it to "foobar", use attr (foo, "bar" ) <- foobar . Time for some code. Because we have not mentioned this explicitly yet, assigning a value "bar" to an object "foo" (which defines the object if it does not exist yet) is done in R by foo <- bar or (less commonly) by bar -> foo . Those who use foo = bar are misguided. Jan de Leeuw 102A_1 UCLA Department of Statistics 1 > a <- 1 2 > typeof (a) 3 [1] "double" 4 > a <- as . integer (a) 5 > typeof (a) 6 [1] "integer" 7 > as . complex (a) 8 [1] 1+0i 9 > as . logical (a) 10 [1] TRUE 11 > is . complex (a) 12 [1] FALSE Jan de Leeuw 102A_1 UCLA Department of Statistics Objects can be printed (to the terminal) using print (foo) . Print is a generic function, which means it checks what type of object it is given and then it calls a print function specific for that object.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}