Vous êtes sur la page 1sur 12
snronors Programming with R: Data Types and Structures < Programming with R (../) > (/12- (4 supp- supp- factors/index.html) call- stack Data Types and Structures © Overview Teaching: 45 min ‘+ What are the different data types in R? + What ate the aifferent data structures in R? + How dol access data within the various data structures? Objectives + Expose learners tothe diferent data types in R. + Lear how to create vectors of afferent types, + Be able to check the type of vector. + Leam about missing data and other special values. + Getting familar withthe different data structures (lists, matrices, data frames}. Understanding Basic Data Types in R To make the best ofthe R language, you'll need a strong understanding of the basie data types and data structures and how to operate on those, \Very important to understand because these are the objects you will manipulate on a day-to-day basis in R. Dealing with object, conversions is one of the most common sources of frustration for beginners, Everything in Ris an object, R.has 6 (although we will nt discuss the raw class for this workshop) atomic vector types. + character + numeric (eal or decimal) + integer + logical + complex. By atomic, we mean the vector only holds data ofa single type. + character: a", “swe + numeric: 2, 15.5 integer: 2. (the L tells R to store this as an integer) + logical: TaUE , FALSE + complex: 144i (complex numbers with real and imaginary parts) R provides many functions to examine features of vectors and other objects, for example + elass(). -what kind of object iit (high-tevel)? + typeof) - what is the objec’s data type (low-level)? hitpsiewcarpentry.githujol-novice-inlammation/13-supp-data-siuctures! ane. snronors Programming with R: Data Types and Structures + ength() - how long is it? What about two dimensional objects? + attributes() - dows it have any metadata? # exanpte x & “dataser typeof (x) | attributes(x) [om ; [ vx ero» [ eet» [ors 2 & as.nunerie(y) [ vxoren Rhas many data structures. These include + atomic vector + list + matrix + data frame + factors Atomic Vectors ‘A vector isthe most common and basic data structure in R and is pretty much the workhorse of R, Technically, vectors can be one of two types: hitpsdewcarpentry.githubiol-novice-nlammation/13-supp-data-siuctures! ane snronors Programming with R: Data Types and Structures + atomic vectors + lists although the term "vector" most commonly refers tothe atomic types not to lists. The Different Vector Modes ‘A vector isa collection of elements that are most commonly of made character , logical , integer of nuneric ‘You can create an empty vector with vector() . (By default the mode is logical . You can be more explicit as shown in the examples below.) Itis more common to use direct constructors such as character() , nuseric() , etc. [ sx [unnne= Janne | [1] FALSE FALSE FALSE FALSE FALSE You can also create vectors by directly specifying their content, Rwill then guess the appropriate mode of storage forthe vector, For instance [rece will create a vector x of mode nuneric . These are the most common kind, and are treated as double precision real numbers. you wanted to explicily create integers, you need to add an t to each element (or coerce to the integer type using as.integer() ) ‘While using quoted text will create a vector of mode character hitpsdewcarpentry.githubiol-novice-nlammation/13-supp-data-siuctures! ane

Vous aimerez peut-être aussi