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.

There are three main ways of adding a variable. Similar to the case of addingobservations, you can use either the cbind() function or the indices. We illustrateboth methods later in this section.You also can use the dollar sign to add an extra variable. Imagine that Grannyasked you to add the number of baskets of her friend Gabrielle to the data frame.First, you would create a vector with that data like this:> baskets.of.Gabrielle baskets.df$Gabrielle head(baskets.df, 4)Granny Geraldine Gabrielle1st 12 5 112nd 4 4 53rd 5 2 64th 6 4 7Adding multiple variables using cbindAs we mention earlier, you can pretend your data frame is a matrix and usethe cbind() function to do this. Contrary to when you use rbind() on data frames,you don’t even need to worry about the row or column names. Let’s create a newdata frame with the goals for Gertrude and Guinevere. You can combine both into adata frame by typing the following code in the editor and running it in the console:> new.df

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

Saved successfully!

Ooh no, something went wrong!