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.

In logit(as.numeric(x)/100) : x not between 0 and 1Not only does the function return NA when it should, but it also gives you awarning that can help with debugging other functions that use the logit() functionsomewhere in the body.Recognizing the Mistakes You’re Sure to MakeDespite all the debugging tools you have at your disposal, you need someexperience to quickly find pesky bugs. But some mistakes are fairly common, andchecking whether you made any of these gives you a big chance of pinpointing theerror easily. Some of these mistakes come from default behavior of R you didn’ttake into account; others are just the result of wool gathering. But every Rprogrammer has made these mistakes at one point, and so will you.Starting with the wrong dataProbably the most common mistakes in R are made while reading in data fromtext files using read.table() or read.csv(), as you do in Chapter 12. Manymistakes result in R throwing errors, but sometimes you only notice somethingwent wrong when you look at the structure of your data. In the latter case youoften find that some or all variables are converted to factors when they reallyshouldn’t be (for example, because they should contain only numerical data).When R gives errors or the structure of your data isn’t what you think it shouldbe, check the following:Did you forget to specify the argument header=TRUE? If so, R will see thecolumn names as values and, as a result, convert every variable to a factor as italways does with character data in a text file.Did you have spaces in your column names or data? The read.table()function can interpret spaces in, for example, column names or in string data asa separator. You then get errors telling you ‘line x did not have y elements’.Did you have a different decimal separator? In some countries, decimalsare separated by a comma. You have to specifically tell R that’s the case byusing the argument dec=”,” in the read.table() function.

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

Saved successfully!

Ooh no, something went wrong!