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.

data frame with character vectors or converting a character matrix to a dataframe. This can be a nasty cause of errors in your code if you’re not aware ofit. If you make it a habit to always specify the stringsAsFactors argument,you can avoid a lot of frustration.Naming variables and observationsIn the previous section, you select data based on the name of the variablesand the observations. These names are similar to the column and row names of amatrix, but there are a few differences as well. We discuss these in the nextsection.Working with variable namesVariables in a data frame always need to have a name. To access the variablenames, you can again treat a data frame like a matrix and use the functioncolnames() like this:> colnames(employ.data)[1] “employee” “salary” “startdate”But, in fact, this is taking the long way around. In case of a data frame, thecolnames() function lets the hard work be done internally by another function, thenames() function. So, to get the variable names, you can just use that functiondirectly like this:> names(employ.data)[1] “employee” “salary” “startdate”Similar to how you do it with matrices, you can use that same function toassign new names to the variables as well. For example, to rename the variablestartdate to firstday, you can use the following code:> names(employ.data)[3] names(employ.data)[1] “employee” “salary” “firstday”Naming observations

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

Saved successfully!

Ooh no, something went wrong!