0% found this document useful (0 votes)
4 views

Mod 2 Summary Table

The document provides a comprehensive overview of data types, vectors, matrices, arrays, lists, and data frames in R programming. It includes definitions, examples, and outputs for various operations and functions associated with these data structures. Key points cover creation, manipulation, and accessing elements within these structures.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Mod 2 Summary Table

The document provides a comprehensive overview of data types, vectors, matrices, arrays, lists, and data frames in R programming. It includes definitions, examples, and outputs for various operations and functions associated with these data structures. Key points cover creation, manipulation, and accessing elements within these structures.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Summary Table:

Data Definition Examples Key Points


Type

Numeric Default type for decimal x <- 10.5 Even integers are stored
and integer values. as numeric.

Integer Explicit whole numbers y <- Truncates decimal part,


using as.integer(). as.integer(3.14) errors on invalid input.

Complex Numbers with real and z <- 3 + 4i Handles imaginary math,


imaginary parts. e.g., sqrt(-1).

Logical Boolean values (TRUE, a > b, TRUE & Useful for conditional
FALSE) from comparisons. FALSE logic.

Character String values with text paste("Hello", Supports formatting and


manipulation functions. "World") substring operations.

Topic Details Examples Output

Creating Use vector() to create vectors of a vector("numeric", 3) [1] 0 0 0


Vectors specific type and length.

numeric(3) [1] 0 0 0

Generating Use seq(), seq.int(), seq_len(), or seq(1:5) [1] 1 2 3 4 5


Sequences seq_along() to generate
sequences.

seq.int(5, 12) [1] 5 6 7 8 9


10 11 12

seq.int(10, 5, -1.5) [1] 10.0 8.5


7.0 5.5

seq_len(7) [1] 1 2 3 4 5
67

p <- c(3, 4, 5, 6) [1] 1 2 3 4


seq_along(p)

Length of Use length() to find or set the length(1:7) [1] 7


Vector vector length.
length(c("a", "b")) [1] 2

nchar(c("aa", "ccc", [1] 2 3 4


"eeee"))

length(s) <- 3 [1] 1 2 3


s

length(s) <- 8 [1] 1 2 3 NA


s NA NA NA
NA

Naming Use names() to assign or retrieve names(x) <- c("a", a b \n 1 2


Elements names of vector elements. "b")
x

s <- 1:3 a b c \n 1 2 3
names(s) <- c("a",
"b", "c")
s

Accessing Use indices (1-based) or names. x[1] [1] 1


Elements Negative indices exclude
elements.

x[-2] [1] 1 3 4 5

x["a"] a \n 1

Invalid Indices Returns NA for out-of-range or x[10] [1] NA


invalid indices.

Combining Use c() to combine vectors. Mixed c(c(1, 2), c("a", "b")) [1] "1" "2"
Vectors types are coerced to a common "a" "b"
type.

Arithmetic Arithmetic is performed element- x+y [1] 7 14 18


Operations wise. Shorter vectors are recycled
to match the length.

x*y [1] 10 48 81

x-y [1] 3 2 0

x*y [1] 10 48 81

Repeating Use rep(), rep.int(), or rep_len() to rep(1:3, 4) [1] 1 2 3 1 2


Elements repeat elements in a vector. 3123123
rep(1:3, each = 4) [1] 1 1 1 1 2
2223333

rep(1:3, times = 1:3) [1] 1 2 2 3 3


3

rep(1:3, length.out = [1] 1 2 3 1 2


9) 3123

rep.int(1:3, 4) [1] 1 2 3 1 2
3123123

rep_len(1:3, 9) [1] 1 2 3 1 2
3123

Here’s a detailed breakdown for Matrices and Arrays in R with examples and outputs, similar
to the previous format:

Topic Details Examples Output

Creating Arrays Use array() function to x <- array(1:24, dim = A 4x3x2 array with
create an array, c(4, 3, 2), dimnames = labeled dimensions
passing a vector of list(c("a", "b", "c", (a, b, c, d for rows, e,
values and vector of "d"), c("e", "f", "g"), f, g for columns, h, i
dimensions. c("h", "i"))) for depth). Example
output shown in the
question.

Creating Use matrix() function m <- matrix(1:12, d e f g \n a 1 4 7 10 \n


Matrices to create a matrix, nrow = 3, dimnames b 2 5 8 11 \n c 3 6 9
passing nrow or ncol = list(c("a", "b", "c"), 12
argument for rows and c("d", "e", "f", "g")))
columns.

Array as Matrix Arrays can also be m1 <- array(1:12, dim d e f g \n a 1 4 7 10 \n


created with two = c(3, 4), dimnames = b 2 5 8 11 \n c 3 6 9
dimensions, list(c("a", "b", "c"), 12
functioning as c("d", "e", "f", "g")))
matrices.

By Row in Use byrow = TRUE to m <- matrix(1:12, d e f g \n a 1 2 3 4 \n


Matrix fill the matrix row- nrow = 3, byrow = b 5 6 7 8 \n c 9 10 11
wise; by default, TRUE, dimnames = 12
matrices are filled list(c("a", "b", "c"),
column-wise. c("d", "e", "f", "g")))

Matrix dim() returns the dim(x) dim(m) [1] 4 3 2 [1] 3 4 [1] 3


Dimensions dimensions of a matrix nrow(m) ncol(m) [1] 4
or array; nrow() and
ncol() return the
number of rows and
columns.

Length of length() returns the length(x) length(m) [1] 24 [1] 12


Array/Matrix total number of
elements in a matrix or
array.

Changing Use dim() to change dim(m) <- c(6, 2) Matrix reshaped into
Dimensions the dimensions of an 6x2
array or matrix.

Fetching Names Use rownames(), rownames(m1) [1] "a" "b" "c" [1] "d"
colnames(), and colnames(m1) "e" "f" "g" [[1]] [1] "a"
dimnames() to get dimnames(x) "b" "c" "d" [[2]] [1]
row, column, and "e" "f" "g"
dimension names.

Extracting Use M[n, m] to extract M[2, 3] M[2, ] M[, 3] [1] 6 [1] 4 5 6 [1] 3 6 9
Elements the element at the nth M[, c(1, 3)] M[c(1, 3), [,1] [,2] \n [1,] 1 3 \n
row and mth column. ] [2,] 4 6 \n [3,] 7 9 [,1]
[,2] [,3] \n [1,] 1 2 3
\n [2,] 7 8 9

Matrix Use t() to transpose a t(M) r1 r2 r3 \n c1 1 4 7 \n


Transpose matrix by swapping its c2 2 5 8 \n c3 3 6 9
rows and columns.

Column and Use cbind() to cbind(M1, M2) [,1] [,2] [,3] \n [1,] 2 8
Row Binding combine columns and rbind(M1, M3) 3 \n [2,] 4 10 6 \n [3,]
rbind() to combine 6 12 9 [,1] [,2] \n [1,]
rows. 2 8 \n [2,] 4 10 \n [3,]
6 12 \n [4,] 4 8

Deconstructing Use c() to flatten a c(M1) [1] 2 4 6 8 10 12


Matrix matrix into a vector.
Element-wise Operators like +, -, *, M1 + M2 M1 * M2 [,1] [,2] \n [1,] 5 19 \n
Operations and / work element- [2,] 10 11 \n [3,] 15
wise on matrices. 17 [,1] [,2] \n [1,] 6 88
\n [2,] 24 10 \n [3,] 54
60

Matrix Use %*% to multiply M1 %*% M2 [,1] [,2] \n [1,] 54 106


Multiplication matrices (requires \n [2,] 72 146 \n [3,]
conformable sizes). 90 186

Element-wise The ^ operator works M2^-1 [,1] [,2] \n [1,]


Power element-wise for 0.3333333
exponentiation on 0.11111111 \n [2,]
matrices. 0.1666667
0.09090909

Inverse of a Use solve() to find the solve(M2) [,1] [,2] \n [1,] -


Matrix inverse of a matrix. 0.5238095 0.4285714
\n [2,] 0.2857143 -
0.1428571

Matrices n arrays

Summary Table with Outputs

Point Description Example Output

Creating a Use list() to create lists. L <- list(c(9,1, 4, 7, 0), [[1]] [1] 9 1 4 7 0
List Lists can contain different matrix(c(1,2,3,4,5,6), [[2]] [,1] [,2] [1,] 1
types of elements like nrow = 3)) 4 [2,] 2 5 [3,] 3 6
vectors, matrices, or even
other lists.

Naming List Name the elements in a names(L) <- c("Num", $Num [1] 9 1 4 7 0
Elements list using names(). "Mat") $Mat [,1] [,2] [1,] 1
4 [2,] 2 5 [3,] 3 6

Nested Lists Lists can be nested, is.recursive(L) returns TRUE


meaning they can contain TRUE
other lists or complex
objects. Use is.recursive()
to check.
Accessing Use [] for slicing or [[]] to L[1] returns sublist, L[[1]] L[1] [[1]] [1] 9 1 4 7
Elements access elements directly. returns the first element 0 L[[1]] [1] 9 1 4 7
directly 0

Length of Use length() to find the length(L) returns 2 2


List number of elements in a
list. Other functions like
dim() return NULL for
lists.

Arithmetic Arithmetic works only if Operations like L1 + L2 If L1 and L2 are


on Lists elements of the list are of are possible if elements numeric, L1 + L2
the same type. Not are numeric. would output
typically recommended element-wise sum.
for lists.

Access by Lists can be accessed by L1[1:2], L1[c(TRUE, L1[1:2] $l1 [1] 8 9 1


Index index, logical values, or FALSE)], L1[["Num"]] $l2 [,1] [,2] [1,] 1 3
element names. [2,] 2 4
L1[["Num"]] [1] 9 1
470

Using $ Access named elements L$Num [1] 9 1 4 7 0


Operator directly using the $
operator.

Converting Lists can be converted unlist(L1) l11 l12 l13 l21 l22
Lists into vectors using l23 l24 l25 78 90
functions like unlist(), 21 11 22 33 44 55
as.numeric(),
as.character().

Combining Use c() to combine c(L1, L2) L1 $l1 [1] 78 90 21


Lists multiple lists into one. $l2 [1] 11 22 33 44
55 [[3]] [1] “aaa”
[[4]] [1] “bbb”

I hope this makes everything clearer! Let me know if you need any more details.

Here's a breakdown of Data Frames in R, explained pointwise with examples and outputs in
a table format:

Summary Table with Outputs for Data Frames

Point Description Example Output


Creating a Data A data frame is created a = c(1, 2, 3); b = c("a", df1 a b d 1 a TRUE
Frame using data.frame(). It "b", "c"); d = c(TRUE, 2 b FALSE 3 c TRUE
can store vectors of FALSE, TRUE); df1 =
different types but data.frame(a, b, d)
equal length.

Naming Rows Row names can be df1 = data.frame(a, b, d, df1 one 1 a TRUE
manually set using the row.names = c("one", two 2 b FALSE
row.names argument. "two", "three")) three 3 c TRUE

Functions on Various functions like rownames(df1) one two three


Data Frames rownames(),
colnames(),
dimnames(), etc., are
used to get details
about a data frame.

colnames(df1) abd

dimnames(df1) [[1]] "one" "two"


"three" [[2]] "a"
"b" "d"

nrow(df1) 3

ncol(df1) 3

dim(df1) 33

Handling Data frames can handle df2 <- data.frame(x = 1, df2 x y y.1 1 1 2 4 1
Different vectors of different y = 2:3, y = 4:7) 2351266127
Length Vectors lengths by recycling 7
shorter ones.

Using check.names = FALSE df3 <- data.frame("BaD df3 BaD.col


check.names prevents invalid col" = c(1:5), !@#$%^&* 1 1
Argument column names from "!@#$%^&*" = c("aaa")) aaa 2 2 aaa 3 3 aaa
being modified.

Accessing a Cells are accessed mtcars[2, 3] [1] 160


Specific Cell using row and column
indices or names in
square brackets [].

mtcars["Mazda RX4 [1] 160


Wag", "disp"]
Checking Use nrow() and ncol() nrow(mtcars) 32
Number of to get the number of
Rows and rows and columns in a
Columns data frame.

ncol(mtcars) 11

Previewing the Use head() to preview head(mtcars) Displays the first 6


First Few Rows the first few rows of a rows of the mtcars
data frame. data frame.

Accessing a Use double square mtcars[["hp"]] [1] 110 110 93 110


Specific Column brackets [[]] or $ to 175 105 245 ...
access a column.

mtcars$hp [1] 110 110 93 110


175 105 245 ...

mtcars[, 4] [1] 110 110 93 110


175 105 245 ...

Accessing To retrieve multiple mtcars[c("mpg", "hp")] mpg hp Mazda


Multiple columns, use [] with a RX4 21.0 110
Columns vector of column Mazda RX4 Wag
names. 21.0 110 ...

Accessing a Rows are accessed mtcars[6, ] Valiant 18.1 6 225


Specific Row using single square 105 2.76 3.46 ...
brackets with row
index and a comma.

mtcars[c(6, 18), ] Valiant 18.1 6 225


105 2.76 3.46 ...
Fiat 128 32.4 4
78.7 66 4.08 2.20
...

Subset Use subset() to fetch subset(D, y < 10 & z, x) x1a2b4d


Function rows based on
conditions and select
columns.

Transpose a Use the t() function to t(D) t(D) x "a" "b" "c"
Data Frame transpose a data "d" "e" "f" y 3 4 7
frame. 8 12 15 z TRUE
TRUE FALSE TRUE
FALSE TRUE

Row and Use rbind() to add rows cbind(D, E) x y z x1 y1 z1 1 a 3


Column Bind and cbind() to add TRUE aaa 9 TRUE 2
Functions columns. Columns b 4 TRUE bbb 12
must match for rbind(). FALSE ...

rbind(D, F) x y z 1 a 3 TRUE 2
b 4 TRUE 3 c 7
FALSE ...

Merge Data Use merge() to merge merge(D, F, by = "x", all x y.x z.x y.y z.y 1 a
Frames two data frames based = TRUE) 3 TRUE 9 TRUE 2 b
on common columns. 4 TRUE 12 FALSE ...

Summary Use functions like colSums(G[, 1:2]) x 26 y 66


Functions on colSums(), rowSums(),
Data Frames colMeans(), and
rowMeans() on
numeric data frames.

rowMeans(G[2:4, ]) 16 17 18

This table summarizes the creation, manipulation, and various operations on data frames in
R, including examples and their outputs. Let me know if you need further clarification on any
point!

Here’s a detailed pointwise explanation of factors and strings in R, as well as the examples,
with output:

2.6 Factors

Point Explanation Example Output

Definition Factors are used for gender <- c("female", gender stores
storing categorical data "male", "female") categorical values
like gender, status, etc. like "female" and
They behave like both "male".
character and integer
vectors depending on
the context.
Creating You can create factors gender <- [1] female male
Factors using the factor() factor(c("female", "male", female male Levels:
function, which takes a "female", "male")) female male
character vector as an
argument.

Levels of Levels are unique levels(gender) [1] "female" "male"


Factors values in a factor. You
can view them using
the levels() function
and count them with
nlevels().

Changing You can change the levels(gender) <- c("M", gender becomes M
Levels factor levels using "F") FMF
levels() or relevel().

Dropping Use droplevels() to diet$eat <- levels(diet$eat)


Unused remove unused levels droplevels(diet$eat) becomes "fruit"
Levels from a factor.

Ordered Factors can be ordered rating_ord <- ordered(val, rating_ord levels:


Factors (e.g., for ratings). Use levels = c("Outstanding", Outstanding <
ordered() or the "Excellent", "Very Good", Excellent < Very
ordered = TRUE "Good", "Bad")) Good < Good < Bad
argument in factor().

Summarizing Use cut() to convert age_group <- cut(age, [1] (15,25] (15,25]
Numeric Data numeric data into seq.int(15, 55, 10)) (25,35] (25,35] ...
categorical data (e.g.,
age groups).

Interaction of The interaction() interaction(fac1, fac2) [1] one.a one.b


Factors function combines two one.c ...
or more factors into a
new factor.

2.7 Strings

Point Explanation Example Output

Creating Strings are c("String 1", "String 2") ["String 1", "String 2"]
Strings stored as
character
vectors,
created using
c().

Concatenating Use paste() or paste("Pine", "Apple") "Pine Apple"


Strings paste0() to
join strings
together.
paste() adds
space by
default, while
paste0()
doesn’t.

Custom You can paste("Pine", "Apple", "Pine-Apple"


Separator specify a sep = "-")
separator in
paste() using
sep.

Collapse The collapse paste(c("Pine", "Red"), "Pine-Apple, Red-Apple"


Strings argument in "Apple", sep = "-",
paste() joins all collapse = ", ")
elements of a
vector into
one string.

Convert to toString() toString(1:10) "1, 8, 27, 64, 125, 216, 343,


String converts a 512, 729, 1000"
number vector
to a string.

Cat Function cat() is similar cat("Red", "Pine", Red Pine Apple


to paste() but "Apple")
prints without
quotes and
separators.

Formatting The formatC() formatC(4.567) "4.567"


Strings function
formats
numbers and
displays them
as strings.
String sprintf() sprintf("The number "The number 1 in the list is =
Formatting formats strings %d in the list is = %f", 4.567000"
with sprintf() and numbers 1, 4.567)
together.

String Case Use toupper() toupper("The cat is on "THE CAT IS ON THE WALL"
Conversion and tolower() the Wall")
to convert a
string to
uppercase or
lowercase.

Substring Use substring() substring("The cat is on "e cat is"


Extraction or substr() to the wall", 3, 10)
extract a part
of a string.

Splitting Use strsplit() strsplit("I like Banana, [1] "I" "like" "Banana,"
Strings to split a string Orange and "Orange" "and" "Pineapple"
based on a Pineapple", " ")
delimiter.

Working with Use getwd() to getwd() "C:/Users/admin/Documents"


Paths get the current
working
directory,
setwd() to
change it, and
file.path() to
construct
paths.

Get Filename Use basename("C:/Program "R.exe"


and Directory basename() to Files/R/R-
get the file 3.3.0/bin/R.exe")
name and
dirname() to
get the
directory
name.

Relative Paths Relative paths path.expand("~") "C:/Users/admin/Documents"


like . (current
dir), .. (parent
dir), and ~
(home dir) can
be expanded
using
path.expand().

These are the points explained with the respective examples and their outputs for both
factors and strings in R.

2.8 Dates and Times in R

R has a rich set of functions and classes to work with dates and times, which are essential in
data analysis. Below is a pointwise explanation of key concepts, examples, and their outputs.

2.8.1 Date and Time Classes

1. POSIXct:

o POSIXct stores date and time as the number of seconds since the "epoch"
(1970-01-01 00:00:00 UTC).

o It is ideal for storing and performing operations on dates and times.

2. POSIXlt:

o POSIXlt stores date and time as a list of individual components like seconds,
minutes, hours, day of the month, etc.

o It is best for extracting specific components of date-time (e.g., hour, minute,


etc.).

3. Date:

o The Date class only stores the date (without time), represented as the
number of days since January 1, 1970.

o It is useful when time is not significant.

Example 1: Using Sys.time() to get the current date and time

Sys.time()

• Output:

• [1] "2017-05-11 14:31:29 IST"

Example 2: Converting POSIXct to POSIXlt


t1 <- Sys.time() # POSIXct time

t2 <- as.POSIXlt(t1) # Convert to POSIXlt

• Output:

o t1: "2017-05-11 14:39:39 IST"

o t2: "2017-05-11 14:39:39 IST"

Example 3: Accessing components of POSIXlt

t2$sec # Seconds component

t2[[ "min" ]] # Minutes component

t2$hour # Hour component

t2$mday # Day of the month component

t2$wday # Day of the week component

• Output:

• [1] 39.20794

• [1] 39

• [1] 14

• [1] 11

• [1] 4

Example 4: Converting POSIXlt to Date

t3 <- as.Date(t2)

t3

• Output:

• [1] "2017-05-11"

2.8.2 Date Conversions

1. Using strptime():

o To parse date and time from strings into POSIXlt format.

o Format is specified using format codes (e.g., %H for hours, %M for minutes).

Example 1: Converting a string into a date-time object


date1 <- strptime("22:15:45 22/08/2015", "%H:%M:%S %d/%m/%Y")

date1

• Output:

• [1] "2015-08-22 22:15:45 IST"

Example 2: Invalid format string

date2 <- strptime("22:15:45 22/08/2015", "%H:%M:%S %d-%m-%Y")

date2

• Output:

• [1] NA

Example 3: Format string explanation:

• %H = Hour (24-hour clock)

• %M = Minutes

• %S = Seconds

• %d = Day of the month

• %m = Month

• %Y = Year (4 digits)

2.8.3 Time Zones

1. Time Zone Specification:

o You can specify the time zone while parsing date strings using strptime() or
strftime().

Example 1: Formatting a date using strftime()

strftime(Sys.Date(), "It’s %I:%M%p on %A %d %B, %Y.")

• Output:

• [1] "It’s 12:00AM on Thursday 11 May, 2017."

2. Getting Time Zone Information:

o Sys.timezone() returns the system's current time zone.

o Sys.getlocale("LC_TIME") provides the locale settings related to time.


Summary Table

Concept Explanation Example Output

POSIXct Stores date and time as the Sys.time() "2017-05-11


number of seconds since 14:31:29 IST"
"epoch" (1970-01-01 00:00:00
UTC). Useful for calculations.

POSIXlt Stores date and time as a list of as.POSIXlt(Sys.time()) "2017-05-11


individual components (e.g., 14:39:39 IST"
seconds, minutes, hours). Useful
for extracting parts of the date-
time.

Date Stores date as the number of as.Date(as.POSIXlt(Sys.time())) "2017-05-11"


days since 1970-01-01. Useful
when time isn't significant.

strptime() Parses date-time string into strptime("22:15:45 "2015-08-22


POSIXlt format using a format 22/08/2015", "%H:%M:%S 22:15:45 IST"
string. %d/%m/%Y")

Invalid If the format string doesn't strptime("22:15:45 NA


match the date-time string, it 22/08/2015", "%H:%M:%S
strptime()
returns NA. %d-%m-%Y")
Format

strftime() Converts date into a formatted strftime(Sys.Date(), "It’s "It’s 12:00AM


string. %I:%M%p on %A %d %B, %Y.") on Thursday
11 May,
2017."

Sys.timezone() Returns the system’s time zone. Sys.timezone() "Asia/Kolkata"

Sys.getlocale Returns the locale settings for Sys.getlocale("LC_TIME") "C"


time-related data.
("LC_TIME")

This table summarizes the key points from the explanation and gives you an easy reference
for each concept and function, along with their expected output.

You might also like