Lecture 03 - 102A_3_ho

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

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

View Full Document Right Arrow Icon
UCLA Department of Statistics Course 102A Introduction to Computational Statistics with R Part III: R Objects 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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 16

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

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online