12.07.2015 Views

R dummies

R dummies

R dummies

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

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

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

Although the row names of the data frames new.df and baskets.df differ, Rwill ignore this and just use the row names of the first data frame in the cbind()function, as you can see from the output of the following code:> head(cbind(baskets.df, new.df), 4)Granny Geraldine Gabrielle Gertrude Guinevere1st 12 5 11 3 62nd 4 4 5 5 93rd 5 2 6 2 74th 6 4 7 1 3When using a data frame or a matrix with column names, R will use those asthe names of the variables. If you use cbind() to add a vector to a data frame, Rwill use the vector’s name as a variable name unless you specify one yourself, asyou did with rbind().If you bind a matrix without column names to the data frame, R willautomatically use the column numbers as names. That will cause a bit of troublethough, because plain numbers are invalid object names and, hence, more difficultto use as variable names. In this case, you’d better use the indices.Whenever you want to use a data frame and don’t want to continuouslyhave to type its name followed by $, you can use the functions with() andwithin(), as explained in Chapter 13. With the within() function, you also caneasily add variables to a data frame.Combining Different Objects in a ListIn the previous sections, you discover how much data frames and matrices aretreated alike by many R functions. But contrary to what you would expect, dataframes are not a special case of matrices but a special case of lists. A list is a verygeneral and flexible type of object in R. Many statistical functions you use inChapters 14 and 15 give a list as output. Lists also can be very helpful to groupdifferent types of objects, or to carry out operations on a complete set of differentobjects. You do the latter in Chapter 9.Creating a list

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

Saved successfully!

Ooh no, something went wrong!