R - Hello World (Data Types and Vector)

  • Start : Install R and R Studio 

  • Five basic Classes of Objects(data types) in R
    • character
    • numeric (if you give 1 for variable, it will give numeric Object)
    • integer (you could give variable x with 1L for giving integer Object)
    • complex
    • logical (Ture or False)
  • Inf and NaN
    • Inf means infinite, for example, 1/0 will give you Inf
    • NaN means Not a Number, for example, 0/0 will give you NaN
  • Comment
    • # indicates comment
    •   x <- 100 # this is comment  
      
  • "Hello World"
    • Give variable x "Hello World" and print it to the console
    •  > x <- "Hello World"  # <- symbol is the assignment operator
       > print(x)  
       [1] "Hello World"  
      
  • Vector : A basic Object contains the same Classes of Objects
    • In "Hello World" example, x is a Vector and the first element is "Hello World"
    • Create Vector using c() function & vector() function
    •  > x <- c(0.5, 0.6)  
       > x  # print x
       [1] 0.5 0.6  
       > x <- vector("numeric", length = 10)  
       > x  # print x
        [1] 0 0 0 0 0 0 0 0 0 0  
      
    • What if we create vector with different Objects? A: Coercion will occur
    •  > y <- c(1.7, "a")  
       > y  # 1.7 will be changed to character "1.7"
       [1] "1.7" "a"   
      
    • Connect string with paste() function
    •  > a <- c("Blog","address","is")  
       > a  
       [1] "Blog"  "address" "is"     
       > paste(a,collapse=" ")  
       [1] "Blog address is"  
       > b <- c(a,"parklize.blogspot.com")  
       > b  
       [1] "Blog"         "address"        "is"            
       [4] "parklize.blogspot.com"  
       > paste(b,collapse=" ")  
       [1] "Blog address is parklize.blogspot.com"  
       > c <- paste("Blog","address","is","parklize.blogspot.com",sep=" ")  
       > c  
       [1] "Blog address is parklize.blogspot.com"  
      
    • Check vectors are identical or not..
    •  > x <- c(1:5)  
       > y <- 1:5  
       > identical(x,y)  
       [1] TRUE  
       > names(x) <- c("a","b","c","d","e")  
       > identical(x,y)  
       [1] FALSE  
      
  • Explicit Coercion
    • Object could be coerced from one class to another class with as.* function
    •  > y <- 1.7  
       > class(y)  
       [1] "numeric"  
       > y <- as.character(y)  # explicitly coerce to character Object
       > class(y)  
       [1] "character"  
      
  • Get help for function
    • str("function name") will get brief information and ?"function name" to get more details
    •  > str(matrix)  # check matrix() function in brief
       function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)   
       > ?matrix  # get help page on the matrix() function
  • Get help for operator
    • If you want to know what the operator ":" does...use ?':' and it will open the help page.
    •  > ?':'  
       starting httpd help server ... done  
      
    • List variables in your workspace
    •  > x <- 1:4  
       > ls()  
       [1] "x"  
      

No comments:

Post a Comment