2 & 3.units Flashcards

1
Q

b

Print()

Paste()

A
  1. 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
  2. ## Used to concatenate or join two or more elements By using “;” , “+”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Datatypes

A

class() and typeof()– Method is used to check the data type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Vectors

A
  1. list of items that are of same type
  2. c()–used to combines or concatenates items and are seperated by “,”
  3. 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

op..n on vectors

A
  1. 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
  2. Modifying Vector
    By using slicing techniques and assignment operator(<-) Weekend modify the elements
    –vectorname[id] <- “value”
  3. Deleting the vector
    We can delete the vector by just assigning null to it
    x<- null
  4. Adding elements to Hector
    Buy append method– Defaultly added last position
  5. Creating a vector
    append(x, val, index(optional))
  6. length()
  7. sort()
  8. mathematical functions
    min()
    max()
    sqrt()
    abs()
    sum()
  9. Operators on vectors
    arithmetic op(Include the topic of recycling of vectors)
    sp operators(%in%, x%%2)
    Relational operators
    logical operators
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

length.out

A

length.out(gives those many items in vector)
-used in seq as such

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

vector recycling

A
  1. When operation suc as addition or subtraction is to be performed on unequal length vectors vector recycling takes place
  2. Each element of the vector with shorter length Recycled with the elements of larger vector
  3. The vector with a small length will be repeated as long as the operation completes on the longer vector
  4. 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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Function to find missing values in vectors

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Filtering and Subsetting in R

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

R-arrays

A
  1. Data objects which allow us to store data in more than two dimensions In rows and columns.
  2. Created with the help of array() function
  3. 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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

matrices

A
  1. Two dimensional data set with columns and rows
  2. 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)
  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)]
  4. 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
  5. 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
  6. Check if an Item Exists: %in%
  7. Number of Rows and Columns: dim()
  8. length()
  9. loop–for loop
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

operations on matrix

A
  1. addition subtraction multiplication and divisions can be performed–Both the mattresses length should be same
  2. Matrix multiplication–%*% *
  3. Transpose-t() and crossprod()
  4. Rows and columns sums–rowSums(vector) colSums(vec)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

reshaping operators

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

lists

A
  1. list can Contains different Data types whereas vector contains only single data
  2. Collection of ordered and changeable data is called as list
  3. list(vec)
  4. access: []
  5. Change Item Value:
    eg:
    thislist <- list(“apple”, “banana”, “cherry”)
    thislist[1] <- “blackcurrant”
    thislist
  6. lenght()
  7. Naming a list - Assigning names to the list items helps in effective accessing
    a<- list(va1=100, var=20)
  8. Check if Item Exists: %in%
  9. add items:append()
    eg:thislist <- list(“apple”, “banana”, “cherry”)
    append(thislist, “orange”)
    Eg:
    thislist <- list(“apple”, “banana”, “cherry”)
    append(thislist, “orange”, after = 2)
  10. merging lists–list()
  11. unlist()- Converts list into vector
  12. 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
  13. range of list items–indexes with :
    thislist <- list(“apple”, “banana”, “cherry”, “orange”, “kiwi”, “melon”, “mango”)
    (thislist)[2:5]
  14. loop–for
    thislist <- list(“apple”, “banana”, “cherry”)
    for (x in thislist) {
    print(x)
    }
  15. Join Two Lists:c() function
    eg:
    list1 <- list(“a”, “b”, “c”)
    list2 <- list(1,2,3)
    list3 <- c(list1,list2)
    list3
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Special functions In lists of R

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Recursive in list

A

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”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Dataframe

A
  1. Data is displayed in the format of table
  2. Data frames can have different types of data inside it but each column should contain same type of data
  3. Columns names should be non empty
  4. Row name should be unique
  5. data.frame()
    eg:
    Data_Frame <- data.frame (
    Training = c(“Strength”, “Stamina”, “Other”),
    Pulse = c(100, 150, 120),
    Duration = c(60, 30, 45)
    )
    Data_Frame
    - We can create head are using names() Function
    - We can give member of rows and columns by nrow() and ncol()
  6. Summarize the Data: summay(dataframename)
  7. accessing:
    [ ]
    [[ ]]
    $
  8. add /combine rows–rbind()
  9. add/combine cols–cbind()
  10. remove rows/cols–c(-1)
  11. Number of rows and columns: dim()
  12. lenght()
17
Q

c() in marices, datframes and lists

A

matrices and dataframes– Remove columns or rows
Lists– Combines or concatenates the lists

18
Q

Special function in Data frame

A

dim(): Returns or sets the dimension of a data frame
str(dataframe): Shows the structure of a data frame and lists variables and their types
rownames(dataframe)- Gives the names of all the rows
colnames()- Gives the names of all the columns
summary(): Provides summary statistics on the columns of a data frame
subset() To create subsets of a data frame
nrow(): Shows the length of a data frame
ncol(): Shows the number of columns in a data frame
apply(): Applies a function to rows or columns of a data frame
lapply(): Applies a function to elements of a list or vector
sapply(): Applies a function to elements of a list or vector and simplifies the output
tapply(): Applies a function to levels of a factor vector
merge() Mergers two data films

19
Q

variable

A
  1. Variable is a piece of computer memory containing some information inside it
  2. Weaker consider it as a Empty box with a name where we can store anything
  3. Variables value can be changed depending upon the conditions or information passed
    naming a vari
    A variable can store any object(Any data tape)
    1) A variable can store any object(Any data tape) so the valid name can consist letters numbers and dot or underlying characters
    2) it should only start with letter or dot followed by text or numbers
    assigning Values to the variable
    with <-
    Finding variables
  4. ls()
  5. ls(all.names=TRUE)
    Deleting the variables
    rm(var1, var2..)
    rm(list=ls())– Deletes all variables
20
Q

seq()

A
  1. A sequence is a set of related numbers dates events etc that follow each other in a particular order
  2. r Directly provides seq() func With a default difference as 1’’
  3. seq(from=, to=, by=, length=)
21
Q

rep()

A
  1. rep() Is used to replicate the values in the vector for specified times
  2. It is very powerful feature in R which helps the user to create a set of values in easy manner
    rep(x, times= n)

()

22
Q

control st
loops

A

Control statements
1. If statement
2. If-else statements
3. ifelse(ifcondition, if–print, else–print)
eg: ifelse(a==3, ‘‘true’’,”false”)
4. nested if
5. switch(expr, case1, case2…)
eg: switch(2, “red”, “pink”, “blue”)
pink

loops
1) Forloop—
for(val in seq) {
st
}
2) While loop(Test expression)
{ st
}
3) Repeat loop
Repeat loop is used to iterate over the block of coal multiple number of times
It does not have any condition check to exit the loop so we need to give condition explicitly inside the body of the loop and use the break statement to exit the loop
repeat{
st
if(cond) {———– To exit the loop
brk
}
}

Loop control statements
break
Used inside the loop to stop the iteration and flow the control outside the loop when the condition satisfied

next
next statement is useful when we want to skip the current iteration of the loop without terminating the loop
eg:
x <- 1:10
for(val in x)
{
if(val==3){
next
}
print(val)
}