10.07.2015 Views

Using R for Introductory Statistics : John Verzani

Using R for Introductory Statistics : John Verzani

Using R for Introductory Statistics : John Verzani

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Data 17> ebay[−1] # all but the first[1] 88.3 90.2 93.5 95.2 94.7 99.2 99.4 101.6> ebay[−(1:4)] # all but the 1st – 4th[1] 95.2 94.7 99.2 99.4 101.6Accessing by names In R, when the data vector has names, then the values can beaccessed by their names. This is done by using the names in place of the indices. Asimple example follows:> x = 1:3> names(x) = c("one","two","three") # set the names> x["one"]one1Parentheses <strong>for</strong> functions; square brackets <strong>for</strong> data vectors The usage of parentheses,(), and square brackets, [], can be confusing at first. To add to the confusion, lists will usedouble square brackets [[]]. It helps to remember that R uses parentheses <strong>for</strong> functionsand square brackets <strong>for</strong> data objects.1.3.1 Assigning values to data vectorWe can assign values to a data vector element by element using indices. The simplestcase, x [i]=a, would assign a value of a to the ith element of x if i is positive. If i is biggerthan the length of the vector x, then x is enlarged. For example, to change the first entryin ebay to 88.0 we could do> ebay[1] =88.0> ebay[1] 88.0 88.3 90.2 93.5 95.2 94.7 99.2 99.4 101.6We can assign more than one value at a time. The case x[vec] ebay[10:13]=c(97.0,99.3,102.0,101.8)> ebay[1] 88.0 88.3 90.2 93.5 95.2 94.7 99.2 99.4 101.6 97.0[11] 99.3 102.0 101.8Data recycling If y is shorter than the values specified by vec, its values are recycled.That is, y is repeated enough times to fill out the request. If y is too long, then it istruncated.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!