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.

<strong>Using</strong> R <strong>for</strong> introductory statistics 136> unclass(f) # shows how f is stored[1] 1 2 3 4 5attr(,"levels”)[1] “a” “b” “c” “d” “e”> as.vector(f) # coerce to vector type[1] “a” “b” “c” “d” “e”> as.integer(f) # coerce to integers[1] 1 2 3 4 5The unclass() function shows that a factor is stored using an internal coding involvingnumbers. The attribute “levels” gives the levels. The coercion to a vector gives acharacter vector in this case; the coercion to an integer returns the internal codes.The final one can cause confusion. Consider this example:> g = factor(2:4)> g[1] 2 3 4Levels: 2 3 4> as.numeric(g)[1] 1 2 3> as.numeric(as.character(g))[1] 2 3 4The as. numeric() command by itself returns the codes when applied to a factor. To getthe levels as numbers, we convert to character, then to numeric:> as.numeric(as.character(x))[1] 2 3 4As a convenience, when factors are used to label graphs, say with the labels= argument ofthe text() function, this conversion is done automatically.Coercing vectors, data frames, and listsThere are “as.” functions to coerce data storage from one type to another. But they can’tdo all the work.Coercing a vector to a data frame If we want to coerce a vector into a data frame wecan do it with the function as.data. frame(). If x is a vector, then as.data.frame(x) willproduce a data frame with one column vector. By default, strings will be coerced tofactors.We may want to create a matrix from our numbers be<strong>for</strong>e coercing to a data frame.This can be achieved by setting the dim() attribute of the data vector. The dim() functiontakes a vector where each component specifies the size of that dimension. As usual, rowsfirst, then columns.> x = 1:8> dim(x)=c(2,4) #2 rows 4 columns> x # column by column[,1] [,2] [,3] [,4]

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

Saved successfully!

Ooh no, something went wrong!