Sunday, October 21, 2012

Part4: Debugging in R

  Debugging tools are built with R do not part of any package. There are couple of indications that something is not right. It could be a diagnostic message something to happed.Basically three type of main indications.
  1. message: A generic notification/diagnostic message produced by the message function; execution of the function continues 
  2. warning: An indication that something is wrong but not necessarily fatal;execution of the function continues generated by the warning function 
  3. error: An indication that a fatal problem has occurred; execution stops;produced by the stop function
condition: A generic concept for indicating that something unexpected can occur; programmers can create their own conditions

So this is our basic warning,  take a log of negative number

>log(-1)
[1] NaN
Warning message:
In log(-1) : NaNs produced

Tuesday, October 9, 2012

Part3: Reading and Writing Data in R


Reading Data   

Large data objects will usually be read as values from external files rather than entered during an R session at the keyboard. R input facilities are simple and their requirements are fairly strict and even rather inflexible. If variables are to be held mainly in data frames, as we strongly suggest they should be, an entire data frame can be read directly with the read.table() function. There is also a more primitive input function, scan(), that can be called directly. There are a few principal functions reading data into R. 
  1. read.table, read.csv, for reading tabular data

    The read.table function is one of the most commonly used functions for reading data. It has a few important arguments:
    • file, the name of a file, or a connection
    • header, logical indicating if the file has a header line
    • sep, a string indicating how the columns are separated
    • colClasses, a character vector indicating the class of each column in the dataset
    • nrows, the number of rows in the dataset
    • comment.char, a character string indicating the comment character
    • skip, the number of lines to skip from the beginning
    • stringsAsFactors, should character variables be coded as factors

    For small to moderately sized datasets, you can usually call read.table without specifying any other arguments
    data <- read.table("foo.txt")

    R will automatically skip lines that begin with a # and figure out how many rows there are (and how much memory needs to be allocated). Figure what type of variable is in each column of the table. Telling R all these things directly makes R run faster and more efficiently. read.csv is identical to read.table except that the default separator is a comma.