Sunteți pe pagina 1din 38

Syllabus

What is R?

The R Environment

Design of the R system

The R console

R Studio

Lets begin!

Downloading and installing R and RStudio

Getting help

Packages in R

R Commands and Case Sensitivity

Expressions

Objects

Sorting

Generating regular sequences

Working directory

The “pass-by-value” and “pass-by-reference” in R

Coercion

Operations on Matrices

Working with date and time

Logical Operators

Practice

Basics of R
Syllabus
Introduction
Installing R and RStudio: R Versions, Download and install R, Installing Packages, Loading
Packages, Updating R and its Packages.
User Interface, Packages and Help: The R Console, RStudio, Getting Help.
R Packages: Listing Packages in Local Libraries, Loading Packages, Package
Repositories, Finding and Installing Packages Inside R and other Repositories,
The R Language
Overview: Expressions, Objects, Symbols, Functions, Special Values.
R Objects: Vectors, Integers, Character, Logicals, Complex and Raw, Attributes of Objects,
Matrices, Arrays, Class, Dates and Time, Factors, Coercion, Lists, Data Frames, Changing
values, Logical Subsetting, Boolean Operators, Missing Information and removing NAs.
R Environment: Symbols, Working with Environments, The Global Environment.
Working with Data in R
Loading and Saving data in R: Entering Data within R, Data Editor (RStudio), Datasets in R,
Working Directory, The read Family, HTML data links, R Files, Saving R Files, Excel
Spreadsheets and R, Loading files from other programs.
Preparing Data: Combining, Transforming, Binning, Subsetting, Cleaning, Sorting and
Summarizing Data.
Graphics
Base Graphics: Scatter Plots, Time Series Plots, Bar Plots, Histogram, Box-Plots,
Customizing Charts.

What is R?
R is an open-source software environment for statistical computing and graphics.
R compiles and runs on Windows, Mac OS X, and numerous UNIX platforms (such as Linux).
R is open source software. Other software like STATA may cost on an average of $1000 annual
license fee.

The R Environment
Features of R
An effective data handling and storage facility.
A suite of operators for calculations on arrays, in particular matrices.
A collection of tools for data analysis.
Graphical facilities for data analysis.
A well developed, simple, and effective programing language.
The R system is a software environment for statistical computing and graphics.
The term “environment” is intended to characterize R as a fully planned and coherent system,
rather than an incremental accretion of very specific and inflexible tools, as is frequently the case
with other data analysis software.
R is an implementation of S language.
S is a language that was developed by John Chambers and others at Bell Labs.
S was initiated in 1976 as a statistical analysis environment.
R was created in New Zealand in the year 1991 by Ross Ihaka and Robert Gentleman.
The first version R 1.0.0 was released in the year 2000 after Ross and Robert agreed to make it a
free software under GNU General Public License in 1995.
The name “GNU” is a recursive acronym for “GNU’s Not Unix.” See https://gnu.org
(https://gnu.org)
Usually, there is an official release of R twice a year.
In S, statistical analysis is usually done as a series of steps with intermediate results being
stored in objects, Thus whereas SPSS and SAS will give copious output from a regression
analysis, R will give minimal output and store the results in a fit object for subsequent
interrogation by further R functions.
Design of the R system
R system is divided into 2 conceptual parts –
The “base” R system that you download from CRAN https://cran.r-project.org/
(https://cran.r-project.org/)
Everything else.
The “base” package contains the most fundamental functions used to run R.
The other packages can be downloaded and installed as per user requirements; for eg. AER
package is required for econometrics.

The R console

Fig 1:The R Console

The R console is a tool that allows you to type commands into R and see how the R system responds.
The commands that you type into the console are called expressions. By default, R will display a
greater-than sign (“>”) in the console (at the beginning of a line, when nothing else is shown) when R is
waiting for you to enter a command into the console. R is prompting you to type something, so this is
called a prompt. For example, suppose that you typed 17 + 3 on the console, you would see something
similar to this:
Fig 2:Simple operations in R Console

R Studio
R Studio Integrated Development Environment (IDE) is a powerful and productive user interface
for R.
Like R, it is free and multi-platform. It can be downloaded from https://www.rstudio.com/
(https://www.rstudio.com/)
RStudio is a separate open-source project that brings many powerful coding tools together into
an intuitive, easy-to-learn interface.
The RStudio program can be run on the desktop or through a web browser.
The desktop version is available for Windows, Mac OS X, and Linux platforms
Fig 3:RStudio

Lets begin!
You can now unistall the R and RStudio already installed in your computers so that you can actually
have a fresh start.

Downloading and installing R and RStudio


Getting help
To get a help on a specific function say “solve”, the command is

help(mean)
# or
?mean()
# or
??mean

For a feature specified by special characters, the argument must be enclosed in double or single
quotes making it a character string. This is also necessary for a few words with syntatic meaning
including if , for and function .

help("[["); help("if")
?"[["

Packages in R
A package is a related set of functions, help files, and data files that have been bundled together. For
example, the stats package contains functions for doing statistical analysis. Some packages are
included in R, other packages are available from public package repositories. You can also make your
own packages!

Inorder to get the list of packages loaded by default use the command;

getOption("defaultPackages")

## [1] "datasets" "utils" "grDevices" "graphics" "stats" "methods"

# if you wish to see the list of currently loaded packages


## (.packages())

# to see all the packages available


(.packages(all.available = TRUE))
## [1] "abind" "AER" "askpass" "assertthat"
## [5] "backports" "base64enc" "BH" "broom"
## [9] "callr" "car" "carData" "cellranger"
## [13] "cli" "clipr" "colorspace" "crayon"
## [17] "crosstalk" "curl" "data.table" "digest"
## [21] "dplyr" "ellipsis" "evaluate" "fansi"
## [25] "forcats" "Formula" "generics" "ggdendro"
## [29] "ggformula" "ggplot2" "ggrepel" "ggstance"
## [33] "glue" "gridExtra" "gtable" "haven"
## [37] "highr" "hms" "htmltools" "htmlwidgets"
## [41] "httpuv" "httr" "jsonlite" "kableExtra"
## [45] "knitr" "labeling" "later" "latticeExtra"
## [49] "lazyeval" "leaflet" "lme4" "lmtest"
## [53] "lubridate" "magrittr" "maptools" "markdown"
## [57] "MatrixModels" "mime" "minqa" "mosaic"
## [61] "mosaicCore" "mosaicData" "munsell" "nloptr"
## [65] "openssl" "openxlsx" "pbkrtest" "pillar"
## [69] "pkgconfig" "plogr" "plyr" "png"
## [73] "prettyunits" "processx" "progress" "promises"
## [77] "ps" "purrr" "quantreg" "R6"
## [81] "raster" "RColorBrewer" "Rcpp" "RcppEigen"
## [85] "readr" "readxl" "rematch" "reshape2"
## [89] "rio" "rlang" "rmarkdown" "rprojroot"
## [93] "rstudioapi" "rvest" "sandwich" "scales"
## [97] "selectr" "shiny" "sourcetools" "sp"
## [101] "SparseM" "stringi" "stringr" "sys"
## [105] "tibble" "tidyr" "tidyselect" "tinytex"
## [109] "utf8" "vctrs" "viridis" "viridisLite"
## [113] "webshot" "withr" "xfun" "xml2"
## [117] "xtable" "yaml" "zeallot" "zip"
## [121] "zoo" "base" "boot" "class"
## [125] "cluster" "codetools" "compiler" "datasets"
## [129] "foreign" "graphics" "grDevices" "grid"
## [133] "KernSmooth" "lattice" "MASS" "Matrix"
## [137] "methods" "mgcv" "nlme" "nnet"
## [141] "parallel" "rpart" "spatial" "splines"
## [145] "stats" "stats4" "survival" "tcltk"
## [149] "tools" "translations" "utils"

# or
library()

To install and load a specific package use the following command. Remember that you have to load the
package everytime when you are in a new R session.

install.packages("AER")
library(AER)

Some packages can contatin data. Inorder to access data from a specific package use the following
command.
## data(package = "AER")
data(SwissLabor, package = "AER") ; head(SwissLabor)

## participation income age education youngkids oldkids foreign


## 1 no 10.78750 3.0 8 1 1 no
## 2 yes 10.52425 4.5 8 0 1 no
## 3 no 10.96858 4.6 9 0 0 no
## 4 no 11.10500 3.1 11 2 0 no
## 5 no 11.10847 4.4 12 0 2 no
## 6 yes 11.02825 4.2 12 0 1 no

Finally, to remove any R package use the function;

remove.packages("AER")

R Commands and Case Sensitivity


Technically R is an ‘expression language’ and is case sensitive, so A and a are different
symbols and would refer to different variables.
Commands are seperated either by a new-line or a semi colon (;).
Comments can be put almost anywhere starting with a hashmark (#).
If a command is not complete at the end of a line, R will give a different prompt, by default + on
second and subsequent lines and continue to read input until the command is syntatically
complete.

Expressions
Examples of expressions in R include assignment statements, conditional statements and arithmetic
expressions.

x <- 1; x

## [1] 1

if (1>2) "yes" else "no"

## [1] "no"

Expressions are composed of objects and fuctions they may be seperated with new lines or
semicolons.

"hello how are you" ;100+50 ; sqrt(100)

## [1] "hello how are you"


## [1] 150

## [1] 10

Objects
An object is a thing that is represented by the computer.

The entries that R creates and manipulates are known as ‘objects’ (more on this later).
During an R session objects are created and stored by name.
The R command objects() can be used to display the names of the objects that are currently
stored within R.
The collection of objects currently stored is called the ‘workspace’.
To remove objects the function rm() is available

objects(); x <- c("my", 12, TRUE); x

## [1] "SwissLabor" "x"

## [1] "my" "12" "TRUE"

objects()

## [1] "SwissLabor" "x"

rm(x); objects()

## [1] "SwissLabor"

R has 5 basic/atomic classes of objects

character: "Hello"
numeric(real numbers/decimal numbers): c(1,2.36) .
integer: 10L
complex : 2+4i
logical: TRUE/FALSE .

The most basic object is a vector. Vectors can contain objects of same class only. However, list() is
an exception and can contain a mixture of objects.

Inf is a special number.

1/Inf
## [1] 0

Inf/Inf

## [1] NaN

NaN (Not A Number) is a value that represents an undefined value NaN may represent missing values
as well.

Objects in R have attributes like names() , dimnames() (for a matrix, array or data frame), class() ,
length() .

class(10L)

## [1] "integer"

class("Hello!")

## [1] "character"

length(12)

## [1] 1

names(12)

## NULL

roll <- c(170, 150, 200)


names(roll) <- "Amit"; names(roll)

## [1] "Amit" NA NA

names(roll) <- c("Amit", "Sourav", "Sneha"); names(roll)

## [1] "Amit" "Sourav" "Sneha"

The things that we are typing/ we type in R are called expressions. <- is called the assignment
operator that assigns a value to a symbol.

num <- 1200


char <- "Hello!"
comp <- 2+4i
You can name an object in R almost anything you want, but there are a few rules.

1. First, a name cannot start with a number.


2. Second, a name cannot use some special symbols,like ^ , ! , $ , @ , + , - , / , or * .

Assignment can also be made using the function

assign("num", 1200 )
num

## [1] 1200

assign("nos", c(1, 1.2, 300, 45)); class(nos)

## [1] "numeric"

nos

## [1] 1.0 1.2 300.0 45.0

Sorting
sort(x) returns a vector of same size as x with the elements arranged in increasing order.

d <- c(10,50,12,63,74,1,89,5,3,6,4)
sort(d)

## [1] 1 3 4 5 6 10 12 50 63 74 89

e <- c("anish", "zebra", "p", "e")


sort(e)

## [1] "anish" "e" "p" "zebra"

To sort in descending order use the argument decreasing = TRUE .

sort(d, decreasing = TRUE)

## [1] 89 74 63 50 12 10 6 5 4 3 1

Generating regular sequences


x <- 1:30; x
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## [24] 24 25 26 27 28 29 30

x <- 30:1; x

## [1] 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8
## [24] 7 6 5 4 3 2 1

x <- c(1,2,3,4,5,6,7,8,9); x

## [1] 1 2 3 4 5 6 7 8 9

x <- seq(1,30); x

## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## [24] 24 25 26 27 28 29 30

x <- seq(-5, 5, by = 0.2); x

## [1] -5.0 -4.8 -4.6 -4.4 -4.2 -4.0 -3.8 -3.6 -3.4 -3.2 -3.0 -2.8 -2.6 -2.4
## [15] -2.2 -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4
## [29] 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2
## [43] 3.4 3.6 3.8 4.0 4.2 4.4 4.6 4.8 5.0

x <- seq(10, 2, length.out = 5); x

## [1] 10 8 6 4 2

x <- seq(length = 51, from = -5, by = 0.2); x

## [1] -5.0 -4.8 -4.6 -4.4 -4.2 -4.0 -3.8 -3.6 -3.4 -3.2 -3.0 -2.8 -2.6 -2.4
## [15] -2.2 -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4
## [29] 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2
## [43] 3.4 3.6 3.8 4.0 4.2 4.4 4.6 4.8 5.0

A related function is rep() which can be used for replicating objects in various ways.

x <- c("hi","hello"); x

## [1] "hi" "hello"

y <- rep(x, times = 2); y


## [1] "hi" "hello" "hi" "hello"

y <- rep(3, times = 10);y

## [1] 3 3 3 3 3 3 3 3 3 3

The paste() function takes an arbitrary number of arguments and concatenates one by one into
character strings. Any numbers given among the arguments are coerced into character strings.

x <- paste("a", 1:10, sep = "")


x

## [1] "a1" "a2" "a3" "a4" "a5" "a6" "a7" "a8" "a9" "a10"

To collapse the output into a single string pass the collapse argument.

labs <- paste(c("x", "y"), 1:10, sep = "")


labs

## [1] "x1" "y2" "x3" "y4" "x5" "y6" "x7" "y8" "x9" "y10"

labs1 <- paste(c("x", "y"), 1:10, sep = "", collapse = "-")


labs1

## [1] "x1-y2-x3-y4-x5-y6-x7-y8-x9-y10"

Working directory
To check the location of your workig derectory, use getwd() and to set a specific folder as your
working directory use setwd("file path") .

The “pass-by-value” and “pass-by-


reference” in R
By defalut R uses “pass-by-value” paradigm. Consider the following example;

x <- 1:10
x*2

## [1] 2 4 6 8 10 12 14 16 18 20

x
## [1] 1 2 3 4 5 6 7 8 9 10

# or consider
y <- x
x*3

## [1] 3 6 9 12 15 18 21 24 27 30

## [1] 1 2 3 4 5 6 7 8 9 10

If you want to use the pass-by-reference paradigm, have a look at the R.oo ,
mutatr and proto packages..

Coercion
1. Implicit Coercion
2. Explicit Coercion

Implicit Coercion occurs when we try to combine objects of two or more different classes. The ordering
is roughly LINCL i.e logical < integer < numeric < complex < character<list .

x <- c(1.7, "a"); class(x)

## [1] "character"

y <- c(TRUE, 2);class(y)

## [1] "numeric"

z <- c("a", TRUE);class(z)

## [1] "character"

We can explicitly coerce objects by using as.*() functions.

x <- c(0,1,2,3,4,5,6)
class(x)

## [1] "numeric"
as.character(x)

## [1] "0" "1" "2" "3" "4" "5" "6"

as.complex(x)

## [1] 0+0i 1+0i 2+0i 3+0i 4+0i 5+0i 6+0i

as.logical(x)

## [1] FALSE TRUE TRUE TRUE TRUE TRUE TRUE

Nonsensical coercion result in NAs. NA stands for Not Available.

x <- c("my", "name ", "is", "khan!")


as.numeric(x)

## Warning: NAs introduced by coercion

## [1] NA NA NA NA

as.complex(x)

## Warning: NAs introduced by coercion

## [1] NA NA NA NA

as.logical(x)

## [1] NA NA NA NA

Operations on Matrices
Matrices are vectors with dimension() attribute.

m <- matrix(1:10, nrow = 2, ncol = 5)


m

## [,1] [,2] [,3] [,4] [,5]


## [1,] 1 3 5 7 9
## [2,] 2 4 6 8 10
dim(m)

## [1] 2 5

length(m)

## [1] 10

class(m)

## [1] "matrix"

Filling direction of a matrix

m <- matrix(1:10, nrow = 2, ncol = 5); m

## [,1] [,2] [,3] [,4] [,5]


## [1,] 1 3 5 7 9
## [2,] 2 4 6 8 10

m <- matrix(1:10, nrow = 2, ncol = 5, byrow = TRUE);m

## [,1] [,2] [,3] [,4] [,5]


## [1,] 1 2 3 4 5
## [2,] 6 7 8 9 10

Row and column binding


This applies to multiple vectors of equal length column-binding and row-biding to create matrices.

x <- 1:5
y <- 6:10
cbind(x,y)

## x y
## [1,] 1 6
## [2,] 2 7
## [3,] 3 8
## [4,] 4 9
## [5,] 5 10

rbind(x,y)
## [,1] [,2] [,3] [,4] [,5]
## x 1 2 3 4 5
## y 6 7 8 9 10

# or
cbind(1:5, 6:10)

## [,1] [,2]
## [1,] 1 6
## [2,] 2 7
## [3,] 3 8
## [4,] 4 9
## [5,] 5 10

Subsetting
Subsetting a matrix with [ will subset objects of the same class

x <- matrix(1:10, 2, 5); x

## [,1] [,2] [,3] [,4] [,5]


## [1,] 1 3 5 7 9
## [2,] 2 4 6 8 10

x[1,2]

## [1] 3

x[2,5]

## [1] 10

x[,2]

## [1] 3 4

x[2,]

## [1] 2 4 6 8 10

x[,1:3] # subset the first 3 columns of the matrix


## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6

x[,c(2,4)] # subset the second and fourth columns

## [,1] [,2]
## [1,] 3 7
## [2,] 4 8

x[1,c(2,4)] # subset the 1st row and second + fourth columns

## [1] 3 7

diag(x) # this works only for a square matrix

## [1] 1 4

When we subset a matrix it is seen that the output is a vector and not in a matrix form. This can be
corrected by setting drop = FALSE argument.

x <- matrix(1:10, 2, 5); x

## [,1] [,2] [,3] [,4] [,5]


## [1,] 1 3 5 7 9
## [2,] 2 4 6 8 10

colnames(x) <- c("Kolkata", "Delhi", "Mumbai", "Chennai", "Bangalore")


row.names(x) <- c("temp", "rain")
x

## Kolkata Delhi Mumbai Chennai Bangalore


## temp 1 3 5 7 9
## rain 2 4 6 8 10

What is the temperature for Delhi?

x[1,3]

## [1] 5

Now let’s see the temperature values in all the cities.

x[1,]
## Kolkata Delhi Mumbai Chennai Bangalore
## 1 3 5 7 9

How does the output look different with the drop argument?

x[1,3, drop = FALSE]

## Mumbai
## temp 5

x[1,, drop = FALSE]

## Kolkata Delhi Mumbai Chennai Bangalore


## temp 1 3 5 7 9

Omitting and Overwriting

x <- matrix(c(0.3,4.5,55.3,91,0.1,105.5,-4.2,8.2,27.9),nrow=3,ncol=3); x

## [,1] [,2] [,3]


## [1,] 0.3 91.0 -4.2
## [2,] 4.5 0.1 8.2
## [3,] 55.3 105.5 27.9

x[,-2]

## [,1] [,2]
## [1,] 0.3 -4.2
## [2,] 4.5 8.2
## [3,] 55.3 27.9

x[-1,3:2] # notice the change in the positions too

## [,1] [,2]
## [1,] 8.2 0.1
## [2,] 27.9 105.5

x[-1,-2] # without first row and second column

## [,1] [,2]
## [1,] 4.5 8.2
## [2,] 55.3 27.9
x[2,] <- 1:3
x[c(1,3),2] <- 900 # overwrites the second and the third row of the second colu
mn
x[c(1,3), c(1,3)] <- c(-7,7)

Replacing diagonals

diag(x) <- c(1,2,3)

Matrix transpose

t(x)

## [,1] [,2] [,3]


## [1,] 1 1 7
## [2,] 900 2 900
## [3,] -7 3 3

Creating identity matrix

i <- diag(3); i

## [,1] [,2] [,3]


## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1

Scalar multiple of a matrix

a <- 2; a; x

## [1] 2

## [,1] [,2] [,3]


## [1,] 1 900 -7
## [2,] 1 2 3
## [3,] 7 900 3

b <- a*x; b

## [,1] [,2] [,3]


## [1,] 2 1800 -14
## [2,] 2 4 6
## [3,] 14 1800 6

a+b; a-b # Matrix addition and substraction


## [,1] [,2] [,3]
## [1,] 4 1802 -12
## [2,] 4 6 8
## [3,] 16 1802 8

## [,1] [,2] [,3]


## [1,] 0 -1798 16
## [2,] 0 -2 -4
## [3,] -12 -1798 -4

Matrix multiplication

Note that matrix multiplication is not elementwise calculation, so the


standard * cannot be used.

dim(x); dim(b)

## [1] 3 3

## [1] 3 3

x%*%b

## [,1] [,2] [,3]


## [1,] 1704 -7200 5344
## [2,] 48 7208 16
## [3,] 1856 21600 5320

Inverse of a matrix

A <- matrix(data=c(3,4,1,2),nrow=2,ncol=2);A

## [,1] [,2]
## [1,] 3 1
## [2,] 4 2

solve(A)

## [,1] [,2]
## [1,] 1 -0.5
## [2,] -2 1.5

An alternative way of creating a matrix using dim() .


m <- 1:10; m

## [1] 1 2 3 4 5 6 7 8 9 10

dim(m) <- c(2,5)


m

## [,1] [,2] [,3] [,4] [,5]


## [1,] 1 3 5 7 9
## [2,] 2 4 6 8 10

The functions rowSums() and colSums() calculate the total for each row and column of a matrix.

a <- c(460.998, 314.4, 290.475, 247.900, 309.306, 165.8)


b <- matrix(a, nrow = 3, byrow = TRUE,
dimnames = list(c("1","2", "3"), c("x", "y")))
b

## x y
## 1 460.998 314.4
## 2 290.475 247.9
## 3 309.306 165.8

totalRow <- rowSums(b); totalRow

## 1 2 3
## 775.398 538.375 475.106

totalCol <- colSums(b); totalCol

## x y
## 1060.779 728.100

Working with date and time


The date() function returns a character string of current date and time.

date()

## [1] "Thu Aug 08 19:13:15 2019"

class(date())
## [1] "character"

The Sys.Date() rerturns the current day in the current time zone.

Sys.Date()

## [1] "2019-08-08"

class(Sys.Date())

## [1] "Date"

Sys.time() returns returns the absolute date-time value.

Sys.time()

## [1] "2019-08-08 19:13:15 IST"

Formatting dates
The follwoing codes are required while formating dates.

Code Output that you get

%d day as number (0-31)

%a abbreviated weekday

A Unabbreviated weekday

m months (00-12)

b abbreviated month

B unabbreviated month

y two digit year

Y Four digit year

d <- Sys.Date()
d

## [1] "2019-08-08"

format(d, "%d %B %Y")


## [1] "08 August 2019"

d1 <- format(d, "%d-%B-%Y")


d1; class(d1)

## [1] "08-August-2019"

## [1] "character"

Creating dates
x <- c("1jan2016", "1feb2017", "1mar2018"); x

## [1] "1jan2016" "1feb2017" "1mar2018"

k <- as.Date(x, "%d %b %Y");k; class(k)

## [1] "2016-01-01" "2017-02-01" "2018-03-01"

## [1] "Date"

d2 <- Sys.time()
d2

## [1] "2019-08-08 19:13:15 IST"

format(d2, "%d-%B-%Y %H:%M:%S %Y")

## [1] "08-August-2019 19:13:15 2019"

There are basically two major data types related to time

1. POSIXct: (Portable Operating System Interface) ct stands for calander time. It is the number of
seconds since 1-Jan-1970. Negative numbers represent the number of seconds before this time.
2. POSIXlt: lt stands for local time and is a named list of vectors representing seconds, minutes,
hour, day, month, year and time zones.

# current time as POSIXct


unclass(Sys.time()) # What does unclass do?

## [1] 1565271796
# as POSIXlt
unclass(as.POSIXlt(Sys.time()))

## $sec
## [1] 15.53448
##
## $min
## [1] 13
##
## $hour
## [1] 19
##
## $mday
## [1] 8
##
## $mon
## [1] 7
##
## $year
## [1] 119
##
## $wday
## [1] 4
##
## $yday
## [1] 219
##
## $isdst
## [1] 0
##
## $zone
## [1] "IST"
##
## $gmtoff
## [1] 19800
##
## attr(,"tzone")
## [1] "" "IST" "+0630"

Working with dates and time with lubridate() .

## install.packages("lubridate")
library(lubridate)

##
## Attaching package: 'lubridate'

## The following object is masked from 'package:base':


##
## date
y <- ymd("2018-08-07");y; class(y)

## [1] "2018-08-07"

## [1] "Date"

dmy(180807)

## [1] "2007-08-18"

ydm(180807)

## [1] "2018-07-08"

You can also add the hour, minute and second information.

dmy_hms("08-09-2019 08:30:00")

## [1] "2019-09-08 08:30:00 UTC"

Time zones.
For example, you have a scheduled admission interview over Skype in a University in London on 25
Sept 2019 at 09:45 am, what time shall it be for you in Kolkata?

Sys.timezone() # System time zone

## [1] "Asia/Calcutta"

OlsonNames() # Time zone directory


## [1] "Africa/Abidjan" "Africa/Accra"
## [3] "Africa/Addis_Ababa" "Africa/Algiers"
## [5] "Africa/Asmara" "Africa/Asmera"
## [7] "Africa/Bamako" "Africa/Bangui"
## [9] "Africa/Banjul" "Africa/Bissau"
## [11] "Africa/Blantyre" "Africa/Brazzaville"
## [13] "Africa/Bujumbura" "Africa/Cairo"
## [15] "Africa/Casablanca" "Africa/Ceuta"
## [17] "Africa/Conakry" "Africa/Dakar"
## [19] "Africa/Dar_es_Salaam" "Africa/Djibouti"
## [21] "Africa/Douala" "Africa/El_Aaiun"
## [23] "Africa/Freetown" "Africa/Gaborone"
## [25] "Africa/Harare" "Africa/Johannesburg"
## [27] "Africa/Juba" "Africa/Kampala"
## [29] "Africa/Khartoum" "Africa/Kigali"
## [31] "Africa/Kinshasa" "Africa/Lagos"
## [33] "Africa/Libreville" "Africa/Lome"
## [35] "Africa/Luanda" "Africa/Lubumbashi"
## [37] "Africa/Lusaka" "Africa/Malabo"
## [39] "Africa/Maputo" "Africa/Maseru"
## [41] "Africa/Mbabane" "Africa/Mogadishu"
## [43] "Africa/Monrovia" "Africa/Nairobi"
## [45] "Africa/Ndjamena" "Africa/Niamey"
## [47] "Africa/Nouakchott" "Africa/Ouagadougou"
## [49] "Africa/Porto-Novo" "Africa/Sao_Tome"
## [51] "Africa/Timbuktu" "Africa/Tripoli"
## [53] "Africa/Tunis" "Africa/Windhoek"
## [55] "America/Adak" "America/Anchorage"
## [57] "America/Anguilla" "America/Antigua"
## [59] "America/Araguaina" "America/Argentina/Buenos_Aires"
## [61] "America/Argentina/Catamarca" "America/Argentina/ComodRivadavia"
## [63] "America/Argentina/Cordoba" "America/Argentina/Jujuy"
## [65] "America/Argentina/La_Rioja" "America/Argentina/Mendoza"
## [67] "America/Argentina/Rio_Gallegos" "America/Argentina/Salta"
## [69] "America/Argentina/San_Juan" "America/Argentina/San_Luis"
## [71] "America/Argentina/Tucuman" "America/Argentina/Ushuaia"
## [73] "America/Aruba" "America/Asuncion"
## [75] "America/Atikokan" "America/Atka"
## [77] "America/Bahia" "America/Bahia_Banderas"
## [79] "America/Barbados" "America/Belem"
## [81] "America/Belize" "America/Blanc-Sablon"
## [83] "America/Boa_Vista" "America/Bogota"
## [85] "America/Boise" "America/Buenos_Aires"
## [87] "America/Cambridge_Bay" "America/Campo_Grande"
## [89] "America/Cancun" "America/Caracas"
## [91] "America/Catamarca" "America/Cayenne"
## [93] "America/Cayman" "America/Chicago"
## [95] "America/Chihuahua" "America/Coral_Harbour"
## [97] "America/Cordoba" "America/Costa_Rica"
## [99] "America/Creston" "America/Cuiaba"
## [101] "America/Curacao" "America/Danmarkshavn"
## [103] "America/Dawson" "America/Dawson_Creek"
## [105] "America/Denver" "America/Detroit"
## [107] "America/Dominica" "America/Edmonton"
## [109] "America/Eirunepe" "America/El_Salvador"
## [111] "America/Ensenada" "America/Fort_Nelson"
## [113] "America/Fort_Wayne" "America/Fortaleza"
## [115] "America/Glace_Bay" "America/Godthab"
## [117] "America/Goose_Bay" "America/Grand_Turk"
## [119] "America/Grenada" "America/Guadeloupe"
## [121] "America/Guatemala" "America/Guayaquil"
## [123] "America/Guyana" "America/Halifax"
## [125] "America/Havana" "America/Hermosillo"
## [127] "America/Indiana/Indianapolis" "America/Indiana/Knox"
## [129] "America/Indiana/Marengo" "America/Indiana/Petersburg"
## [131] "America/Indiana/Tell_City" "America/Indiana/Vevay"
## [133] "America/Indiana/Vincennes" "America/Indiana/Winamac"
## [135] "America/Indianapolis" "America/Inuvik"
## [137] "America/Iqaluit" "America/Jamaica"
## [139] "America/Jujuy" "America/Juneau"
## [141] "America/Kentucky/Louisville" "America/Kentucky/Monticello"
## [143] "America/Knox_IN" "America/Kralendijk"
## [145] "America/La_Paz" "America/Lima"
## [147] "America/Los_Angeles" "America/Louisville"
## [149] "America/Lower_Princes" "America/Maceio"
## [151] "America/Managua" "America/Manaus"
## [153] "America/Marigot" "America/Martinique"
## [155] "America/Matamoros" "America/Mazatlan"
## [157] "America/Mendoza" "America/Menominee"
## [159] "America/Merida" "America/Metlakatla"
## [161] "America/Mexico_City" "America/Miquelon"
## [163] "America/Moncton" "America/Monterrey"
## [165] "America/Montevideo" "America/Montreal"
## [167] "America/Montserrat" "America/Nassau"
## [169] "America/New_York" "America/Nipigon"
## [171] "America/Nome" "America/Noronha"
## [173] "America/North_Dakota/Beulah" "America/North_Dakota/Center"
## [175] "America/North_Dakota/New_Salem" "America/Ojinaga"
## [177] "America/Panama" "America/Pangnirtung"
## [179] "America/Paramaribo" "America/Phoenix"
## [181] "America/Port-au-Prince" "America/Port_of_Spain"
## [183] "America/Porto_Acre" "America/Porto_Velho"
## [185] "America/Puerto_Rico" "America/Punta_Arenas"
## [187] "America/Rainy_River" "America/Rankin_Inlet"
## [189] "America/Recife" "America/Regina"
## [191] "America/Resolute" "America/Rio_Branco"
## [193] "America/Rosario" "America/Santa_Isabel"
## [195] "America/Santarem" "America/Santiago"
## [197] "America/Santo_Domingo" "America/Sao_Paulo"
## [199] "America/Scoresbysund" "America/Shiprock"
## [201] "America/Sitka" "America/St_Barthelemy"
## [203] "America/St_Johns" "America/St_Kitts"
## [205] "America/St_Lucia" "America/St_Thomas"
## [207] "America/St_Vincent" "America/Swift_Current"
## [209] "America/Tegucigalpa" "America/Thule"
## [211] "America/Thunder_Bay" "America/Tijuana"
## [213] "America/Toronto" "America/Tortola"
## [215] "America/Vancouver" "America/Virgin"
## [217] "America/Whitehorse" "America/Winnipeg"
## [219] "America/Yakutat" "America/Yellowknife"
## [221] "Antarctica/Casey" "Antarctica/Davis"
## [223] "Antarctica/DumontDUrville" "Antarctica/Macquarie"
## [225] "Antarctica/Mawson" "Antarctica/McMurdo"
## [227] "Antarctica/Palmer" "Antarctica/Rothera"
## [229] "Antarctica/South_Pole" "Antarctica/Syowa"
## [231] "Antarctica/Troll" "Antarctica/Vostok"
## [233] "Arctic/Longyearbyen" "Asia/Aden"
## [235] "Asia/Almaty" "Asia/Amman"
## [237] "Asia/Anadyr" "Asia/Aqtau"
## [239] "Asia/Aqtobe" "Asia/Ashgabat"
## [241] "Asia/Ashkhabad" "Asia/Atyrau"
## [243] "Asia/Baghdad" "Asia/Bahrain"
## [245] "Asia/Baku" "Asia/Bangkok"
## [247] "Asia/Barnaul" "Asia/Beirut"
## [249] "Asia/Bishkek" "Asia/Brunei"
## [251] "Asia/Calcutta" "Asia/Chita"
## [253] "Asia/Choibalsan" "Asia/Chongqing"
## [255] "Asia/Chungking" "Asia/Colombo"
## [257] "Asia/Dacca" "Asia/Damascus"
## [259] "Asia/Dhaka" "Asia/Dili"
## [261] "Asia/Dubai" "Asia/Dushanbe"
## [263] "Asia/Famagusta" "Asia/Gaza"
## [265] "Asia/Harbin" "Asia/Hebron"
## [267] "Asia/Ho_Chi_Minh" "Asia/Hong_Kong"
## [269] "Asia/Hovd" "Asia/Irkutsk"
## [271] "Asia/Istanbul" "Asia/Jakarta"
## [273] "Asia/Jayapura" "Asia/Jerusalem"
## [275] "Asia/Kabul" "Asia/Kamchatka"
## [277] "Asia/Karachi" "Asia/Kashgar"
## [279] "Asia/Kathmandu" "Asia/Katmandu"
## [281] "Asia/Khandyga" "Asia/Kolkata"
## [283] "Asia/Krasnoyarsk" "Asia/Kuala_Lumpur"
## [285] "Asia/Kuching" "Asia/Kuwait"
## [287] "Asia/Macao" "Asia/Macau"
## [289] "Asia/Magadan" "Asia/Makassar"
## [291] "Asia/Manila" "Asia/Muscat"
## [293] "Asia/Nicosia" "Asia/Novokuznetsk"
## [295] "Asia/Novosibirsk" "Asia/Omsk"
## [297] "Asia/Oral" "Asia/Phnom_Penh"
## [299] "Asia/Pontianak" "Asia/Pyongyang"
## [301] "Asia/Qatar" "Asia/Qostanay"
## [303] "Asia/Qyzylorda" "Asia/Rangoon"
## [305] "Asia/Riyadh" "Asia/Saigon"
## [307] "Asia/Sakhalin" "Asia/Samarkand"
## [309] "Asia/Seoul" "Asia/Shanghai"
## [311] "Asia/Singapore" "Asia/Srednekolymsk"
## [313] "Asia/Taipei" "Asia/Tashkent"
## [315] "Asia/Tbilisi" "Asia/Tehran"
## [317] "Asia/Tel_Aviv" "Asia/Thimbu"
## [319] "Asia/Thimphu" "Asia/Tokyo"
## [321] "Asia/Tomsk" "Asia/Ujung_Pandang"
## [323] "Asia/Ulaanbaatar" "Asia/Ulan_Bator"
## [325] "Asia/Urumqi" "Asia/Ust-Nera"
## [327] "Asia/Vientiane" "Asia/Vladivostok"
## [329] "Asia/Yakutsk" "Asia/Yangon"
## [331] "Asia/Yekaterinburg" "Asia/Yerevan"
## [333] "Atlantic/Azores" "Atlantic/Bermuda"
## [335] "Atlantic/Canary" "Atlantic/Cape_Verde"
## [337] "Atlantic/Faeroe" "Atlantic/Faroe"
## [339] "Atlantic/Jan_Mayen" "Atlantic/Madeira"
## [341] "Atlantic/Reykjavik" "Atlantic/South_Georgia"
## [343] "Atlantic/St_Helena" "Atlantic/Stanley"
## [345] "Australia/ACT" "Australia/Adelaide"
## [347] "Australia/Brisbane" "Australia/Broken_Hill"
## [349] "Australia/Canberra" "Australia/Currie"
## [351] "Australia/Darwin" "Australia/Eucla"
## [353] "Australia/Hobart" "Australia/LHI"
## [355] "Australia/Lindeman" "Australia/Lord_Howe"
## [357] "Australia/Melbourne" "Australia/North"
## [359] "Australia/NSW" "Australia/Perth"
## [361] "Australia/Queensland" "Australia/South"
## [363] "Australia/Sydney" "Australia/Tasmania"
## [365] "Australia/Victoria" "Australia/West"
## [367] "Australia/Yancowinna" "Brazil/Acre"
## [369] "Brazil/DeNoronha" "Brazil/East"
## [371] "Brazil/West" "Canada/Atlantic"
## [373] "Canada/Central" "Canada/Eastern"
## [375] "Canada/Mountain" "Canada/Newfoundland"
## [377] "Canada/Pacific" "Canada/Saskatchewan"
## [379] "Canada/Yukon" "CET"
## [381] "Chile/Continental" "Chile/EasterIsland"
## [383] "CST6CDT" "Cuba"
## [385] "EET" "Egypt"
## [387] "Eire" "EST"
## [389] "EST5EDT" "Etc/GMT"
## [391] "Etc/GMT-0" "Etc/GMT-1"
## [393] "Etc/GMT-10" "Etc/GMT-11"
## [395] "Etc/GMT-12" "Etc/GMT-13"
## [397] "Etc/GMT-14" "Etc/GMT-2"
## [399] "Etc/GMT-3" "Etc/GMT-4"
## [401] "Etc/GMT-5" "Etc/GMT-6"
## [403] "Etc/GMT-7" "Etc/GMT-8"
## [405] "Etc/GMT-9" "Etc/GMT+0"
## [407] "Etc/GMT+1" "Etc/GMT+10"
## [409] "Etc/GMT+11" "Etc/GMT+12"
## [411] "Etc/GMT+2" "Etc/GMT+3"
## [413] "Etc/GMT+4" "Etc/GMT+5"
## [415] "Etc/GMT+6" "Etc/GMT+7"
## [417] "Etc/GMT+8" "Etc/GMT+9"
## [419] "Etc/GMT0" "Etc/Greenwich"
## [421] "Etc/UCT" "Etc/Universal"
## [423] "Etc/UTC" "Etc/Zulu"
## [425] "Europe/Amsterdam" "Europe/Andorra"
## [427] "Europe/Astrakhan" "Europe/Athens"
## [429] "Europe/Belfast" "Europe/Belgrade"
## [431] "Europe/Berlin" "Europe/Bratislava"
## [433] "Europe/Brussels" "Europe/Bucharest"
## [435] "Europe/Budapest" "Europe/Busingen"
## [437] "Europe/Chisinau" "Europe/Copenhagen"
## [439] "Europe/Dublin" "Europe/Gibraltar"
## [441] "Europe/Guernsey" "Europe/Helsinki"
## [443] "Europe/Isle_of_Man" "Europe/Istanbul"
## [445] "Europe/Jersey" "Europe/Kaliningrad"
## [447] "Europe/Kiev" "Europe/Kirov"
## [449] "Europe/Lisbon" "Europe/Ljubljana"
## [451] "Europe/London" "Europe/Luxembourg"
## [453] "Europe/Madrid" "Europe/Malta"
## [455] "Europe/Mariehamn" "Europe/Minsk"
## [457] "Europe/Monaco" "Europe/Moscow"
## [459] "Europe/Nicosia" "Europe/Oslo"
## [461] "Europe/Paris" "Europe/Podgorica"
## [463] "Europe/Prague" "Europe/Riga"
## [465] "Europe/Rome" "Europe/Samara"
## [467] "Europe/San_Marino" "Europe/Sarajevo"
## [469] "Europe/Saratov" "Europe/Simferopol"
## [471] "Europe/Skopje" "Europe/Sofia"
## [473] "Europe/Stockholm" "Europe/Tallinn"
## [475] "Europe/Tirane" "Europe/Tiraspol"
## [477] "Europe/Ulyanovsk" "Europe/Uzhgorod"
## [479] "Europe/Vaduz" "Europe/Vatican"
## [481] "Europe/Vienna" "Europe/Vilnius"
## [483] "Europe/Volgograd" "Europe/Warsaw"
## [485] "Europe/Zagreb" "Europe/Zaporozhye"
## [487] "Europe/Zurich" "GB"
## [489] "GB-Eire" "GMT"
## [491] "GMT-0" "GMT+0"
## [493] "GMT0" "Greenwich"
## [495] "Hongkong" "HST"
## [497] "Iceland" "Indian/Antananarivo"
## [499] "Indian/Chagos" "Indian/Christmas"
## [501] "Indian/Cocos" "Indian/Comoro"
## [503] "Indian/Kerguelen" "Indian/Mahe"
## [505] "Indian/Maldives" "Indian/Mauritius"
## [507] "Indian/Mayotte" "Indian/Reunion"
## [509] "Iran" "Israel"
## [511] "Jamaica" "Japan"
## [513] "Kwajalein" "Libya"
## [515] "MET" "Mexico/BajaNorte"
## [517] "Mexico/BajaSur" "Mexico/General"
## [519] "MST" "MST7MDT"
## [521] "Navajo" "NZ"
## [523] "NZ-CHAT" "Pacific/Apia"
## [525] "Pacific/Auckland" "Pacific/Bougainville"
## [527] "Pacific/Chatham" "Pacific/Chuuk"
## [529] "Pacific/Easter" "Pacific/Efate"
## [531] "Pacific/Enderbury" "Pacific/Fakaofo"
## [533] "Pacific/Fiji" "Pacific/Funafuti"
## [535] "Pacific/Galapagos" "Pacific/Gambier"
## [537] "Pacific/Guadalcanal" "Pacific/Guam"
## [539] "Pacific/Honolulu" "Pacific/Johnston"
## [541] "Pacific/Kiritimati" "Pacific/Kosrae"
## [543] "Pacific/Kwajalein" "Pacific/Majuro"
## [545] "Pacific/Marquesas" "Pacific/Midway"
## [547] "Pacific/Nauru" "Pacific/Niue"
## [549] "Pacific/Norfolk" "Pacific/Noumea"
## [551] "Pacific/Pago_Pago" "Pacific/Palau"
## [553] "Pacific/Pitcairn" "Pacific/Pohnpei"
## [555] "Pacific/Ponape" "Pacific/Port_Moresby"
## [557] "Pacific/Rarotonga" "Pacific/Saipan"
## [559] "Pacific/Samoa" "Pacific/Tahiti"
## [561] "Pacific/Tarawa" "Pacific/Tongatapu"
## [563] "Pacific/Truk" "Pacific/Wake"
## [565] "Pacific/Wallis" "Pacific/Yap"
## [567] "Poland" "Portugal"
## [569] "PRC" "PST8PDT"
## [571] "ROC" "ROK"
## [573] "Singapore" "Turkey"
## [575] "UCT" "Universal"
## [577] "US/Alaska" "US/Aleutian"
## [579] "US/Arizona" "US/Central"
## [581] "US/East-Indiana" "US/Eastern"
## [583] "US/Hawaii" "US/Indiana-Starke"
## [585] "US/Michigan" "US/Mountain"
## [587] "US/Pacific" "US/Pacific-New"
## [589] "US/Samoa" "UTC"
## [591] "W-SU" "WET"
## [593] "Zulu"
## attr(,"Version")
## [1] "2019a"

interviewLondon <- dmy_hms("25-Sep-2019 09:45:00", tz = "Europe/London")


interviewLondon

## [1] "2019-09-25 09:45:00 BST"

interviewKolkata <- with_tz(interviewLondon,tz = "Asia/Calcutta")


interviewKolkata

## [1] "2019-09-25 14:15:00 IST"

So your interview timings are; (a) 2019-09-25 09:45:00 if you are in London,
and (b) 2019-09-25 14:15:00 if you are in Kolkata.

Logical Operators
Operator Interpretation Results
Operator Interpretation Results

& AND (element-wise) TRUE & TRUE = TRUE


TRUE & FALSE = FALSE
FALSE & TRUE = FALSE
FALSE & FALSE = FALSE

&& AND (single comparison) same as above

| OR (element-wise) TRUE | TRUE = TRUE


TRUE | FALSE is TRUE
FALSE | TRUE is TRUE
FALSE | FALSE is FALSE

|| OR (single comparison) same as above

! NOT !TRUE = FALSE


!FALSE = TRUE

Some examples

a <- c(T,F,T,F); a

## [1] TRUE FALSE TRUE FALSE

b <- c(F,T,T,T); b

## [1] FALSE TRUE TRUE TRUE

a&b; a&&b

## [1] FALSE FALSE TRUE FALSE

## [1] FALSE

a|b; a||b

## [1] TRUE TRUE TRUE TRUE

## [1] TRUE

Logical subsetting and extraction


myvec <- c(5,-2.3,4,4,4,6,8,10,40221,-8)
myvec[c(F,T,F,F,F,F,F,F,F,T)] # extracts the negative numbers in the vector
## [1] -2.3 -8.0

myvec<0

## [1] FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE

myvec[myvec < 0] # same operation as above

## [1] -2.3 -8.0

To extract every second elment from the vector starting with the first

myvec[c(T,F)] # the flag vector c(T,F) is recycled if it's too short.

## [1] 5 4 4 8 40221

Relational and logical operators together

myvec[(myvec>0) & (myvec < 1000)]

## [1] 5 4 4 4 6 8 10

Identifying the index positions of a vector

which(myvec < 0)

## [1] 2 10

Let’s apply logical subsetting and extraction in matrices as an example. In the following example we
shall

a. Extract elements from a row or column.


b. Change the elements in the matrix.

A <- matrix(c(0.3,4.5,55.3,91,0.1,105.5,-4.2,8.2,27.9),nrow=3,ncol=3); A

## [,1] [,2] [,3]


## [1,] 0.3 91.0 -4.2
## [2,] 4.5 0.1 8.2
## [3,] 55.3 105.5 27.9

A[c(T,F,F),c(F,T,T)] #extracts the second and the third elements of first row.
c(T, F,F) selects the first row, c(F,T,T) selects the elements from the select
ed first row.
## [1] 91.0 -4.2

A[c(F,T,F),c(F,T,T)] #extracts the second and the third elements of second row.

## [1] 0.1 8.2

A[A<1] <- 100 # Changes the elements in the matrix with values less than 1.
A

## [,1] [,2] [,3]


## [1,] 100.0 91.0 100.0
## [2,] 4.5 100.0 8.2
## [3,] 55.3 105.5 27.9

Practice
1. Create a vector x and assign a numerical value to it.
2. Create another vector y and assign the numbers 1 to 5.
3. Creater a longer vector that contains numbers 1 to 5 ten times.
4. Assign some meaningful names to the following vectors:
c(2, 4, 6, 8, 10, 12, 14, 16, 20)
0
3.141593
c(1, 10, 100, 1000, 10000, 100000)
5. Create vectors that correspond to the following variables names:
BMI
Age
daysPerMonth
firstFivePrimeNumbers
6. Create three vectors that each contain just 1 element with variable names p , q , and r , and
values 1, 2, and 3. Then, create a new vector that contains multiple elements, using the scalars
we just created i.e., create a vector u of length 3, with the subsequent elements of p , q and
r.
7. Create a new vector u with length 96 that contains the elements of u as follows: 1, 2, 3, 1, 2, 3, ….,
1, 2, 3.
8. Suppose the surface area of a circle equals 25, what is the radius?
9. What is the probability density at x=0 of a normally distributed random variable x with mean (mu)
equal to zero, and standard devation (sigma) equal to one?
10. Sort the numbers 10,50,12,63,74,1,89,5,3,6,4 in ascending order.
11. Generate a sequence of 100 numbers between -60 to 45 with a width of 5, and find the mean
value.
12. Use the functions mean() and range() to find the mean and range of:
the numbers 1, 2, . . . , 21
the sample of 50 random normal values, that can be generated from a normaL distribution
with mean 0 and variance 1 using the assignment y <- rnorm(50) .
the columns height and weight in the data frame women. [The datasets package that has
this data frame is by default attached when R is started.]
13. What are the respective effects of the arguments sep and collapse in the paste() function?
14. Create a matrix as depicted in the following table. The row names are the roll numbers of the
students in economics class, and the column names are the codes of the courses. The values
represent the marks obtained out of 50, and are random. You can assume and assign marks.

ECON0101 ECON0102 ECON0201 ECON0791

ECON01 35 25 36 40

ECON02 36 25 35 39

… … … … …

ECON40 40 32 45 25

15. Calculate the mean marks obtained by each student in Q14 and find out which students got the
top and last 3 positions (hint: you can sort the data).
16. What date and time shall your friend in Massachusetts Institute of Technology (MIT) be speaking
with you online if you call him/her now?
17. Create a 4X3 matrix with numbers between -30 and 30 (excluding 0). Extract the first and second
elements from the fourth row. Extract the second and third elements from the second row.
Replace all the negative elements in the matrix with 0.

S-ar putea să vă placă și