R+Supplement+-+2.2+-+Wikibooks+Vectors

R Supplement 2.2- - Statistical Analysis an Introduction using R/R/Vectors 1 Statistical Analysis an Introduction using R/R/Vectors One of the most

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

View Full Document Right Arrow Icon
Statistical Analysis: an Introduction using R/R/Vectors 1 Statistical Analysis: an Introduction using R/R/Vectors One of the most fundamental objects in R is the vector , used to store multiple measurements of the same type (e.g. data variables). There are several different sorts of data that can be stored in a vector. Most common is the numeric vector , in which each element of the vector is simply a number. Other commonly used types of vector are character vectors (where each element is a piece of text) and logical vectors (where each element is either TRUE or FALSE [1] ). In this topic we will use some example vectors provided by the "datasets" package, containing data on States of the USA (see ?state). R is an inherently vector-based program; in fact the numbers we have been using in previous calculations are just treated as vectors with a single element. This means that most basic functions in R will behave sensibly when given a vector as a argument, as shown below. Input: state.area #a NUMERIC vector giving the area of US states, in square miles state.name #a CHARACTER vector (note the quote marks) of state names sq.km <- state.area*2.59 #Arithmetic works on numeric vectors, e.g. convert sq miles to sq km sq.km #... the new vector has the calculation applied to each element in turn sqrt(sq.km) #Many mathematical functions also apply to each element in turn range(state.area) length(state.area) #and some, like this useful one, just return a single value. Result: > state.area #a NUMERIC vector giving the area of US states, in square miles [1] 51609 589757 113909 53104 158693 104247 5009 2057 58560 58876 6450 83557 56400 [14] 36291 56290 82264 40395 48523 33215 10577 8257 58216 84068 47716 69686 147138 [27] 77227 110540 9304 7836 121666 49576 52586 70665 41222 69919 96981 45333 1214 [40] 31055 77047 42244 267339 84916 9609 40815 68192 24181 56154 97914 > state.name #a CHARACTER vector (note the quote marks) of state names [1] "Alabama" "Alaska" "Arizona" "Arkansas" [5] "California" "Colorado" "Connecticut"
Background image of page 1

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

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

This note was uploaded on 10/18/2011 for the course POL POL 51 taught by Professor Haptosthal during the Fall '11 term at UC Davis.

Page1 / 4

R Supplement 2.2- - Statistical Analysis an Introduction using R/R/Vectors 1 Statistical Analysis an Introduction using R/R/Vectors One of the most

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

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