2 & 3.units Flashcards
b
Print()
Paste()
- Unlike other programming languages the output In the R can be printed without print function
-But R Provide print()…. It should be used must in the loops - ## Used to concatenate or join two or more elements By using “;” , “+”
Datatypes
class() and typeof()– Method is used to check the data type
Vectors
- list of items that are of same type
- c()–used to combines or concatenates items and are seperated by “,”
-
seq,decimal seq, logical also can be given–n<-1:10
Classes of vectors
1) Logical vector
2) Character vector
3) Number Vector
4) Integer vector
5) Complex Vector
6) raw vector
Elements of Vector:
1. c()
2. names()– If it is easy to remember alphanumeric representation of index rather than numeric indexes
-so names function provides naming a vector with alpha numerics
names(vectorname)= vector
3. colon(:) We can also give the values to Vector with the colon operator which takes as range of values
4. seq(from=, to= , by= ) Sequence operator also can be used to give values to a vector
5. rep(vector, times)
r<-rep(c(1,2,3)each=3)
r #111222333
op..n on vectors
- Accessing vector elements: Elements can be accessed by indexing the index can be integer logical character
i. Integer vector as index
Indexing in R starts from one rather than 0, Positive indexing and negative indexing exists but cannot be used together
ii. Logical vector index
The position with the logical vector is true is it and this feature is also helped in filtering Vector based on conditions
iii. Character Vector as index
Named index
namex()
access—
vectorname[id]
or
—vectorname[c(1,3)]
or
-ve indexing -
Modifying Vector
By using slicing techniques and assignment operator(<-) Weekend modify the elements
–vectorname[id] <- “value” -
Deleting the vector
We can delete the vector by just assigning null to it
x<- null -
Adding elements to Hector
Buy append method– Defaultly added last position -
Creating a vector
append(x, val, index(optional)) - length()
- sort()
-
mathematical functions
min()
max()
sqrt()
abs()
sum() -
Operators on vectors
arithmetic op(Include the topic of recycling of vectors)
sp operators(%in%, x%%2)
Relational operators
logical operators
length.out
length.out(gives those many items in vector)
-used in seq as such
vector recycling
- When operation suc as addition or subtraction is to be performed on unequal length vectors vector recycling takes place
- Each element of the vector with shorter length Recycled with the elements of larger vector
- The vector with a small length will be repeated as long as the operation completes on the longer vector
- The resultant value of expression is a vector whose length is same as the longer vector and the shorter vector should be multiple of longer vector
eg:
vec1=1:6
vec2=1:2
print(vec1+vec2)
Function to find missing values in vectors
is.na()-for NA(not available)
Returns logical vector with true in the elements location that contains missing values represented by N A
true in the elements location that contains missing values represented by N A It will work on Vector’s lists mattresses and data frames
- We can remove missing values:
na.rm=true
is.nan-for NaN(not a number)
eg:
x<- c(NA, 3, NA)
is.na(x)
Filtering and Subsetting in R
filtering
Getting required values from the vector
a <- vectorname[vec %in% val]
Subsetting is an indexing feature that lets you select and filter observations and variables..
Here are some ways to filter and subset vectors in R:
1. Subsetting with brackets[]:
vectorname[start index : end index]
2. subset() func: This function creates a subset of vectors, matrices, or data frames based on the parameters’ conditions.
subset(x, cond, cols)
4. filter(): This function requires two arguments:
Data frame: The data frame from which the subset should be drawn
Condition statement: Describes how the subset should be drawn from the original data frame
R-arrays
- Data objects which allow us to store data in more than two dimensions In rows and columns.
- Created with the help of array() function
- It takes Vector as an input and create An array and it uses Vector’s values in dim parameter
eg:
array with 1 dim:
arrname1<-c(vector values)
array with more than 1 dim:
arrname<-array(vector, dim=c(rows,cols,o/p times repeat))
Accessing array items:
-by index position–[ ]
Check if an Item Exists
%in% operator
thisarray <- c(1:24)
multiarray <- array(thisarray, dim = c(4, 3, 2))
2 %in% multiarray
length()
loop through array-for
thisarray <- c(1:24)
multiarray <- array(thisarray, dim = c(4, 3, 2))
for(x in multiarray){
print(x)
}
Functions on array
We can do operations through apply function
apply(x, margin, func)
x- array
margin- Name of the data set used
fun- Function applied across the elements of array
eg:
a<- apply(r, c(1), sum)
matrices
- Two dimensional data set with columns and rows
- A matrix can be created with the
i. matrix() function. Specify the nrow and ncol parameters to get the amount of rows and columns:
m<- matrix(c(values), nrow=, ncol= )
ii. With vector
Already declaring Vector and then assigning them in the matrix function in the vector place
x <- matrix(c(a,b,c), nrow=3, ncol=3) - accessing:
specified—[]
whole row after sp-[1,]
whole row before sp-[,1]
more than 1 row-[c(1,2),]
more than 1 col–[, c(1,2)] -
add rows or cols or combine cols and rows:
cbind()–cols
rbind()–rows
eg: to add cols:
m <- matrix(c(“a”, “b”, “c”,”d”), nrow = 2, ncol = 2)
n <- cbind(m, c(“s”, “g”, “r”))
n
eg: to combine rows:
m <- matrix(c(“a”, “b”, “c”,”d”), nrow = 2, ncol = 2)
n<-matrix(“e”,”f”,”g”,”h”),nrow=2, ncol=2)
comb<-rbind(m,n)
comb - remove rows and cols:
c()- Giving minus before the index number in C()
m <- matrix(c(“a”, “b”, “c”,”d”), nrow = 2, ncol = 2)
n <- m(c(-2),c(-1))
n - Check if an Item Exists: %in%
- Number of Rows and Columns: dim()
- length()
- loop–for loop
operations on matrix
- addition subtraction multiplication and divisions can be performed–Both the mattresses length should be same
- Matrix multiplication–%*% *
- Transpose-t() and crossprod()
- Rows and columns sums–rowSums(vector) colSums(vec)
reshaping operators
Here are some R functions that can be used for data reshaping:
cbind(): Combines a vector, matrix, or data frame by columns
rbind(): Combines a vector, matrix, or data frame by rows
melt(): Converts an object into a molten data frame
cast(): Reshapes data in multiple steps to get a desired shape
spread(): “Unbundles” a column into multiple columns
The tidyr and dplyr packages can also be used to reshape data. For example, **parse_json() **from the jsonlite package can parse JSON data into nested lists.
lists
- list can Contains different Data types whereas vector contains only single data
- Collection of ordered and changeable data is called as list
- list(vec)
- access: []
- Change Item Value:
eg:
thislist <- list(“apple”, “banana”, “cherry”)
thislist[1] <- “blackcurrant”
thislist - lenght()
-
Naming a list - Assigning names to the list items helps in effective accessing
a<- list(va1=100, var=20) - Check if Item Exists: %in%
- add items:append()
eg:thislist <- list(“apple”, “banana”, “cherry”)
append(thislist, “orange”)
Eg:
thislist <- list(“apple”, “banana”, “cherry”)
append(thislist, “orange”, after = 2) - merging lists–list()
- unlist()- Converts list into vector
-
dim() - To assign or change the dimensions of the List during creation Of list or later
6.Remove list items: listname[-1]
hislist <- list(“apple”, “banana”, “cherry”)
newlist <- thislist[-1]
newlist - range of list items–indexes with :
thislist <- list(“apple”, “banana”, “cherry”, “orange”, “kiwi”, “melon”, “mango”)
(thislist)[2:5] - loop–for
thislist <- list(“apple”, “banana”, “cherry”)
for (x in thislist) {
print(x)
} - Join Two Lists:c() function
eg:
list1 <- list(“a”, “b”, “c”)
list2 <- list(1,2,3)
list3 <- c(list1,list2)
list3
Special functions In lists of R
c(): Joins or concatenates two lists by considering them as parameter values.
mapply(): Performs operations on multiple lists simultaneously.
list(): Creates a list of elements of different types, such as numeric, string, or vector elements.
Recursive in list
rapply()
eg:
ls <- list(a = 1:5, b = 100:110, c = c(‘a’, ‘b’, ‘c’))
cat(“Whole List: \n”)
print(ls)
cat(“Using replace mode:\n”)
rapply(ls, mean, how = “replace”, classes = “integer”)