Mod 2 Summary Table
Mod 2 Summary Table
Numeric Default type for decimal x <- 10.5 Even integers are stored
and integer values. as numeric.
Logical Boolean values (TRUE, a > b, TRUE & Useful for conditional
FALSE) from comparisons. FALSE logic.
numeric(3) [1] 0 0 0
seq_len(7) [1] 1 2 3 4 5
67
s <- 1:3 a b c \n 1 2 3
names(s) <- c("a",
"b", "c")
s
x[-2] [1] 1 3 4 5
x["a"] a \n 1
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.
x*y [1] 10 48 81
x-y [1] 3 2 0
x*y [1] 10 48 81
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:
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.
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
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
Matrices n arrays
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
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().
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:
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
colnames(df1) abd
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.
ncol(mtcars) 11
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
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 ...
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
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.
Changing You can change the levels(gender) <- c("M", gender becomes M
Levels factor levels using "F") FMF
levels() or relevel().
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).
2.7 Strings
Creating Strings are c("String 1", "String 2") ["String 1", "String 2"]
Strings stored as
character
vectors,
created using
c().
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.
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.
These are the points explained with the respective examples and their outputs for both
factors and strings 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.
1. POSIXct:
o POSIXct stores date and time as the number of seconds since the "epoch"
(1970-01-01 00:00:00 UTC).
2. POSIXlt:
o POSIXlt stores date and time as a list of individual components like seconds,
minutes, hours, day of the month, etc.
3. Date:
o The Date class only stores the date (without time), represented as the
number of days since January 1, 1970.
Sys.time()
• Output:
• Output:
• Output:
• [1] 39.20794
• [1] 39
• [1] 14
• [1] 11
• [1] 4
t3 <- as.Date(t2)
t3
• Output:
• [1] "2017-05-11"
1. Using strptime():
o Format is specified using format codes (e.g., %H for hours, %M for minutes).
date1
• Output:
date2
• Output:
• [1] NA
• %M = Minutes
• %S = Seconds
• %m = Month
• %Y = Year (4 digits)
o You can specify the time zone while parsing date strings using strptime() or
strftime().
• Output:
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.