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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Using</strong> R <strong>for</strong> introductory statistics 112top-level components are data vectors, the length of a data frame is the number ofvariables it contains.> length(ewr) # number of top-levelcomponents[1] 114.2.2 Accessing values in a data frameThe values of a data frame can be accessed in several ways. We’ve seen that we canreference a variable in a data frame by name. Additionally, we see how to accesselements of each variable, or multiple elements at once.Accessing variables in a data frame by nameUp to this point, most of the times that we have used the data in a data frame we have“attached” the data frame so that the variables are accessible in our work environment bytheir names. This is fine when the values will not be modified, but can be confusingotherwise. When R attaches a data frame it makes a copy of the variables. If we makechanges to the variables, the data frame is not actually changed. This results in twovariables with the same names but different values.The following example makes a data frame using data.frame() and then attaches it.When a change is made, it alters the copy but not the data frame.> x = data.frame(a=1:2,b=3:4) # make a data frame> a # a is not thereError: Object "a" not found> attach(x) # now a and b are there> a # a is a vector[1] 1 2> a[1] =5 # assignment> a # a has changed[1] 5 2> x # not x thougha b1 1 32 2 4> detach(x) # remove x> a # a is there andchanged[1] 5 2> x # x is not changeda b1 1 32 2 4The with() function and the data= argument were mentioned in Chapter 1 as alternativesto attaching a data frame. We will use these when it is convenient.

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

Saved successfully!

Ooh no, something went wrong!