Data Science Classroom Series – 12/Dec/2021

Dataframes

  • A dataframe in R is an 2D object where columns can contain data of different classes and types.
  • This is very useful for practical storage
  • Dataframes can be created by using as.data.frame() on applicable objects or column or row binding vectors using cbind.data.frame() or rbind.data.frame() Preview

Variables

  • Variable Assignment: There are two ways to assign a value to a variable, The operators are <- and =, with the first being preffered
x <- 2
x

y = 5
y
  • The arrow operator can be used in other direction
3 -> z

Preview

  • The assignment operation can be used succesively to assign a value to multiple variable simulatenous Preview
  • A more laborious though sometimes necessary way to assign variable is to use assign function
assign("j", 10)
j
  • Naming convention:
    • Variable names can contain any combination of alpha numeric characters with periods(.) and underscores(_). However the variable name cannot start with a number or underscore Preview
  • Removing variables:
    • This is easily done by using remove or its shortcut rm
    • Removing variable will free up memory so that R can store more objects, although it deoes not necessarily free up memory from os. To guarntee that, us gc which performs garbage, releasing unused memory to operting system.
    • R automatically does garbage collection so this function is not essential Preview
  • Ensure we use Google’s R style guide Refer Here
  • Refer Here for assignment operators in r
  • What’s the difference between = and <- Preview

Vectors Contd

  • Vectors in R are special as R is vecorized language, that means operations applied to each element of the vector automatically without needing to loop the vector
  • Examples Preview
  • To create a vector we have use 1:10 which generates consecutive numbers Preview
  • In R we have a length function which will determine number of elements in vector Preview
> all(y>w)
> TRUE
  • nchar => find number of characters Preview

  • Activity 1:

    • We have gamber who has played two games "BlackJack" and "Poker"
    • His daily profit or loss for black jack are
      • Monday => lost 100 $
      • Tuesday => gained 200 $
      • Wednesday => lost 50 $
      • Thursday => lost 100$
      • Friday => gained 200 $
    • His daily profit or loss for poker are
      • Monday => lost 1000 $
      • Tuesday => gained 2000 $
      • Wednesday => lost 500 $
      • Thursday => lost 1000 $
      • Friday => gained 2000 $
  • Represent this in vectors in R Preview

  • Vector Recycling:

    • Repetition of small length as long as completition of operation on long length of vector is called as vector recycling Preview Preview
    • Samples: Preview
  • Naming vectors: Preview Preview

  • Indexing and slicing vectors Preview Preview

  • Lets assume we want to get the values from poker

    • where we have lost
    poker[poker<0]
    
    • where we have gained
    poker[poker>0]
    

    Preview

Function Documentation

  • We can use help() Preview Preview

Things to Come/Learn in Near Future

  • We have simulate coin flip and rolling a dice Preview

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin