12.07.2015 Views

R dummies

R dummies

R dummies

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

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

To get only the number of rows, you use the nrow() function. The ncol()function gives you the number of columns of a matrix.You can find the total number of values in a matrix exactly the same way asyou do with a vector, using the length() function:> length(first.matrix)[1] 12Actually, if you look at the output of the str() function, that matrix looksvery much like a vector. That’s because, internally, it’s a vector with a smallextra piece of information that tells R the dimensions (see the nearby sidebar,“Playing with attributes”). You can use this property of matrices in calculations,as you’ll see further in this chapter.Playing with attributesBoth the names and the dimensions of matrices and arrays are stored in R asattributes of the object. These attributes can be seen as labeled values youcan attach to any object. They form one of the mechanisms R uses to definespecific object types like dates, time series, and so on. They can include anykind of information, and you can use them yourself to add information to anyobject.To see all the attributes of an object, you can use the attributes() function.You can see all the attributes of my.array like this:> attributes(my.array)$dim[1] 3 4 2This function returns a named list, where each item in the list is an attribute.Each attribute can, on itself, be a list again. For example, the attributedimnames is actually a list containing the row and column names of a matrix.You can check that for yourself by checking the output ofattributes(baskets.team). You can set all attributes as a named list as well.You find examples of that in the Help file ?attributes.To get or set a single attribute, you can use the attr() function. Thisfunction takes two important arguments. The first argument is the object you

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

Saved successfully!

Ooh no, something went wrong!