Last active
February 9, 2016 08:12
-
-
Save aravindhebbali/39ff8d3b633ac88d29a6 to your computer and use it in GitHub Desktop.
Gist for the R Data Types tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# assign value 3 to variable radius | |
radius <- 3 | |
# assign value 3.14 to variable pi | |
pi <- 3.14 | |
# compute the area of the circle and assign it to | |
# a variable of the same name | |
area <- pi * radius * radius | |
# access radius | |
radius | |
# access area | |
area | |
# numeric data type | |
# create two variables | |
number1 <- 3.5 | |
number2 <- 3 | |
# test the type of the variables | |
class(number1) | |
class(number2) | |
# check if variable is of type numeric | |
is.numeric(number1) | |
is.numeric(number2) | |
# integer data types | |
# create variable and assign an integer | |
number1 <- 3 | |
class(number1) | |
# specify the variable to be integer | |
number2 <- as.integer(3) | |
class(number2) | |
# test if a variable is an integer | |
is.integer(number1) | |
is.integer(number2) | |
# coerce other data types to type integer | |
number3 <- 3 | |
class(number3) | |
# coerce to type integer | |
number3 <- as.integer(number3) | |
class(number3) | |
# character data | |
name <- 'John' | |
class(name) | |
# returns NA | |
as.integer(name) | |
# logical data | |
x <- c(TRUE, FALSE) | |
as.integer(x) | |
# character data type | |
first_name <- "Jovial" | |
last_name <- 'Mann' | |
first_name | |
last_name | |
# error if quotes not used | |
name <- Tom | |
# test data type | |
is.character(first_name) | |
class(last_name) | |
# logical data types | |
x <- TRUE | |
y <- FALSE | |
# test data types | |
class(x) | |
is.logical(y) | |
# use comparison operators | |
x > y | |
x < y | |
x == y | |
# store output in a new variable | |
z <- x != y | |
class(z) | |
# TRUE and FALSE are represented by 1 and 0 | |
as.logical(1) | |
as.logical(0) | |
as.numeric(TRUE) | |
as.numeric(FALSE) | |
as.logical(c(-2, -1, 0, 1, 1.5)) | |
# coerce other data types to type logical | |
# create variables of different data types | |
age <- as.integer(25) # integer | |
score <- 3.8 # numeric | |
name <- 'John' # character | |
today <- Sys.time() # date/time | |
# coerce the above to type logical | |
as.logical(age) | |
as.logical(score) | |
as.logical(name) | |
as.logical(today) | |
# date and time | |
# returns date | |
Sys.Date() | |
# returns day, month, date, time and year | |
date() | |
# returns date and time | |
Sys.time() | |
# returns timezone | |
Sys.timezone() | |
# different ways to represent date and time | |
# dd-mm-yyyy | |
date <- as.Date("27/09/2015", format = "%d/%m/%Y") | |
date | |
# mm-dd-yyyy | |
date <- as.Date("09/27/2015", format = "%m/%d/%Y") | |
date | |
# dd-mm-yyyy | |
date <- as.Date("September 27,2015", format = "%B %d, %Y") | |
date |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment