Tuesday, 8 August 2017

Data Science Tutorial Part 6 Data Frames

  1. Data frames
  1. In simple words a data frame is a table
  2. Rows are called as observations
  3. Columns are called as variables
  4. Columns can be different data types
  5. One column should store only one datatype data
  6. Create data frame
    1. Generally we are going to create data frame by importing csv files,relational database,excel ..etc
    2. First let us see how to create manually

  7. Structure of the data frame .Notice that above data frame has 3 observations(rows) and 4 variables(columns)

  8. In create data frame include one more parameter stringsAsFactors = FALSE and observe str(emp.data)
  9. Summary of data in data frame

  10. Exercise : create one more dataframe with the name df and with below data
  11. Extract specific column of a data frame using column name

  12. Extract the first two rows and then all columns
  13. Extract the first two columns and then all rows
  14. Extract specific rows and columns
  1. Add a column to data frame

  2. Add row to data frame



  3. Delete a row from data frame

  4. Delete column from data frame

  5. Head(),tail(),dim() functions
  1. Sort() and order() functions
  2. The number of data rows in the data frame is given by the nrow() function
  3. the number of columns of a data frame is given by the ncol() function
  4. We reference a data frame column with the double square bracket "[[]]" operator.
  5. For example, to retrieve the ninth column vector of the built-in data set mtcars, we write mtcars[[9]] or mtcars[["am"]] or mtcars$am or mtcars[,"am"]
For more refer https://cran.r-project.org/doc/manuals/R-intro.html#Data-frames or http://www.r-tutor.com/r-introduction/data-frame

Data Science Tutorial Part 5 Factors

  1. Factors
  1. The term factor refers to a statistical data type used to store categorical variables.
  2. The difference between a categorical variable and a continuous variable is that a categorical variable can belong to a limited number of categories. A continuous variable, on the other hand, can correspond to an infinite number of values.
  3. A good example of a categorical variable is the variable 'Gender' (male and Female) ,Blood Groups (A,B,AB,O),Eye Color (amber, blue,brown, gray, green, hazel, or red),Months names (jan,feb,mar,..etc)



  4. Create a blood group factor
  5. Order levels differently
  6. Rename factor levels
  7. Rename factor levels in customized format
  8. Nominal Vs Ordinal
    1. Nominal
    2. Ordinal
For more information read https://www.stat.berkeley.edu/classes/s133/factors.html or https://cran.r-project.org/doc/manuals/R-intro.html#Factors or https://stat.ethz.ch/R-manual/R-devel/library/base/html/factor.html

Data Science Tutorial Part 4 Matrices

  1. Matrices
  1. Matrix: 2D array of data elements
  2. They contain elements of the same atomic types. Though we can create a matrix containing only characters or only logical values, they are not of much use.
  3. We use matrices containing numeric elements to be used in mathematical calculations.
  4. A Matrix is created using the matrix() function.
  5. The basic syntax for creating a matrix in R
    1. matrix(data, nrow, ncol, byrow, dimnames)
  6. Observe below examples
  7. Read elements of matrix

  8. Matrix arithmetic Calculations
  9. We construct the transpose of a matrix by interchanging its columns and rows with the function t 
  1. Cbind is useful to combine columns

  2. we can combine the rows of two matrices if they have the same number of columns with the help of rbind function

  3. We can deconstruct a matrix by applying the c function, which combines all column vectors into one or converting a matrix into vector
Refer http://www.r-tutor.com/r-introduction/matrix or https://cran.r-project.org/doc/manuals/R-intro.html#Arrays-and-matrices

Data Science Tutorial Part 3 Vectors

  1. Creating Vectors
  1. Vector: 1D array of data elements with same datatype
  2. The c() function can be used to create vectors of objects by concatenating things together.
  1. Note that in the above example, T and F are short-hand ways to specify TRUE and FALSE. However,in general one should try to use the explicit TRUE and FALSE values when indicating logical values.
  2. You can also use the vector() function to initialize vectors.
  1. There are occasions when different classes of R objects get mixed together then R will convert implicitly  into some class.
  1. Objects can be explicitly coerced from one class to another using the as.* functions, if available

  2. Sometimes, R can’t figure out how to coerce an object and this can result in NAs being produced.

  3. Single value variable also vector
  4. Create a named vector for playing cards
  5. Method 1:
  6. Method 2:
  7. Method 3:
  8. One more example : let us say you played cards Monday to Friday and record profit and loss as vector.
For more please refer https://cran.r-project.org/doc/manuals/R-intro.html#Simple-manipulations-numbers-and-vectors or http://www.r-tutor.com/r-introduction/vector

Data Science tutorial Part 2 - Data Types

  1. Data Types
  1. In our project mainly we will use four datatypes
    1. Decimals values like 4.5 are called numerics.
    2. Natural numbers like 4L are called integers. Integers are also numerics.
    3. Boolean values (TRUE or FALSE or NA) are called logical.
    4. Text (or string) values are called characters.
  2. Class () function is useful to know the data type of variable.
  1. ls() function is useful to see list of variables in the workspace
  1. rm() function is useful to remove the variable from work space
  1. Coercion : If possible as.*() functions convert one datatype to other datatype
  2. Is.*() function useful to know the datatype of variable
  1. Complex data type
For more information about datatypes ,please refer http://www.r-tutor.com/r-introduction/basic-data-types

Wednesday, 2 August 2017

Functions in R

  1. Functions

  1. Function accepts N number of inputs and provides one output
  2. Functions works like a black box

  3. Function arguments matching :  by position or by name
  4. Function arguments can have defaults
vec <-c(10,30,40,50,20)
sd(vec)
vec <-c(10,30,40,50,20,NA)
sd(vec)
sd(x=vec,na.rm=TRUE) #by value
sd(vec,TRUE) #by position
sd()

  1. What is the default value of na.rm argument? FALSE
  2. How to see arguments of a function?
  3. Exercise 1: create function with one mandatory argument
    1. Method 1:
    2. Method 2:

  1. Exercise 2: create function with two mandatory arguments
  1. Exercise 3: create function with one mandatory arguments and one optional argument