R Programming DataCamp
Kinds of logical operators in R : 1) AND & 2) OR | 3) NOT ! PS : These operators can be used with vectors as well as matrices
! logical operator negates the the R object. !TRUE gives FALSE !FALSE gives TRUE !0 will give TRUE and not 1. (*Important*)
Difference between & and && amprecent and double amprecent Difference between | and ||
& operator between two vectors results a logical vector in wheich every element is compared. like : car <- c ( TRUE , FALSE , TRUE , FALSE ) truck <- c (TRUE , TRUE , FALSE , FALSE ) now , car & truck will result into a logical vector : TRUE FALSE FALSE FALSE but, car && truck will result into a single result: TRUE because it ONLY compares the *First* elements of the vector. Similarly, || also compares the first element of the two vectors.
What is matrix ?
A matrix is also a collection same type of Data *Arranged in rows and Coloumns* Matrix is 2-D data storage.
How to select specific element from a vector or Any Data frame ?
By the use of Square brackets for example poker_vector [1] selects first element of poker_vector IMPORTANT In R , first element is indexed 1 , and not 0. poker_vector ["Monday"] will show the element assigned to Monday.
How to select multiple Elements from a vector / Data frame ?
For Selecting elements square bracket is generally used. But since we have to select multilple elements, we will use combination functionm poker_vector[c (1,2,5)] This selects 1 , 2 , 5 elements of poker_vector
What can be the R objects in the first argument of the matrix fucntion ?
It can have a vector. Like view < - matrix(c(linkedin , facebook), byrow = TRUE , nrow =2 )
Equality and inequality :Comparison Comparison of strings ? "Cat" > "Cats" FALSE > TRUE
R performs equality on strings based on alphabetical order. Cats comes after Cat. Raining dogs comes after raining , hence "Raining" > "raining dogs" will come out to be false. FALSE under the hood equals 0 in R TRUE is 1 . Hence , FALSE > TRUE will give a result FALSE.
What are the Logical vectors in R ?
The (logical) comparison operators known to R are: < for less than > for greater than <= for less than or equal to >= for greater than or equal to == for equal to each other != not equal to each other
Comparison of vectors Linkedin > facebook
This will give a logical vector having TRUE and FALSE as values
What are vectors ?
Vectors are *one dimensional arrays* that can hold values such as numeric, characters, etc They are simple tools to store data.
Is R case sensitive ?
Yes, R is case sensitive. FALSE is right , false is wrong. Sharad != sharad
what is a good practice to name elements of a vector ?
create a variable to store names. days_vector <- ("Monday","tuesday", "Wednesday", "Thursday","Friday") assign this vector to different vectors by using names function . names(assignment_scores) <- c (days_vector)
Comparison of Matrix
just like comparison of Vectors, comparsion of matrix also results a logial "Matrix" like for example : vlets create a matrix view by using matrix ( ) function. elements ( R objects for this matrix is two vectors linkedin and facebook) view <- matrix ( c (linkedin , facebook) , nrow =2 , byrow = TRUE )
How to create a Matrix ?
matrix ( ) fucntion is used to create a matrix
What are the arguments used in matrix Fucntion ?
matrix ( first argument , second , third ) In the matrix() function: The first argument is the collection of elements that R will arrange into the rows and columns of the matrix. Here, we use 1:9 which is a shortcut for c(1, 2, 3, 4, 5, 6, 7, 8, 9). The second argument byrow indicates that the matrix is filled by the rows. If we want the matrix to be filled by the columns, we just place byrow = FALSE. The third argument nrow indicates that the matrix should have three rows. matrix ( c(1:9) , byrow = TRUE , nrow = 3)
What is a names function ?
names with s in the end , is a fucntion used to assign names to different elements of a vector. names(some_vector) <- c ("Monday" , "Tuesday") assigns names to a vector "Some_vector".
Short method to select multiple elements in vector / Data Frame ?
poker_vector [c (1:5) ] this selects all elements from 1 to 5 including both.
Correct way of initialisng vectors ?
second <- li_df ["day2"] is correct Here we are extracting the second column as vector from the data frame (li_df) (Likedin) This is the wrong way : li_df ["day2"] <- second
how to find sum of all elements of a vector ?
sum ( ) function is used. sum (poker_vector) finds the totoal sum of all ements of a poker_vector.
Tail function
tail (First argument , second argument ) first argument taken in the R object like a vector. second argument takes the number of elements which has to be taken from behind (tail) like: tail ( linkedin , 1 ) will result into the laste lement of the vector linkedin. tail ( linkedin , 2 ) will rsult the last two elements of the vector linkedin.
How a vector is created in R ?
vector is created by combine function : c () Place the vector elements inside ( " " ) my_vector <- c ( 1,2,3)
How to assign a variable ?
x <- 45
Can Comparison (Logical) operators be used on Vectors ?
yes