Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles by Nizamuddin Siddiqui
Page 77 of 196
How to create a random sample with values 0 and 1 in R?
It is known that the random sample can be created by using sample function in R. If we want to create a random sample with values 0 and 1 only then there are three different ways to pass them inside the sample function −Creating a vector of 0 and 1Using 0:1Using c(1,2) directly inside the function.Also, we can set replace argument to TRUE or FALSE based on our requirement.Examplesx2
Read MoreHow to multiply two vectors in R as in mathematics?
In mathematics, when two vectors are multiplied the output is a scalar quantity which is the sum of the product of the values. For example, if we have two vectors x and y each containing 1 and 2 then the multiplication of the two vectors will be 5. In R, we can do it by using t(x)%*%y.Example1x1
Read MoreHow to combine two lists of same size to make a data frame in R?
If we have two lists of same size then we can create a data frame using those lists and this can be easily done with the help of expand.grid function. The expand.grid function create a data frame from all combinations of the provided lists or vectors or factors. For example, if we have two lists defined as List1 and List2 then we can create a data frame using the code expand.grid(List1,List2).ExampleList2
Read MoreHow to multiply a matrix with a vector in R?
When we multiply a matrix with a vector the output is a vector. Suppose we have a matrix M and vector V then they can be multiplied as M%*%V. To understand the step-by-step multiplication, we can multiply each value in the vector with the row values in matrix and find out the sum of that multiplication.Example1M1
Read MoreHow to replicate a vector to create matrix in R?
The matrix can be created by using matrix function in R and if we want to create a matrix by replicating a vector then we just need to focus on the replication. For example, if we have a vector V and we want to create matrix by replicating V two times then the matrix can be created as matrix(replicate(2,V),nrow=2).Example1V1
Read MoreHow to calculate the number of elements greater than a certain value in a vector in R?
In data analysis, sometimes we need to count the number of values that are greater than or less than a certain value, and this certain value could be a threshold. For example, we might have a vector that contain values for blood pressure of people and we might want check how many values are greater than 120. In this type of situation, we can use length function as shown in the below examples.Examplex11])Output[1] 9 Examplex25])Output[1] 93Examplex35])Output[1] 42Examplex40])Output[1] 108Examplex51])Output[1] 107Examplex65])Output[1] 31Examplex71])Output[1] 21Examplex84])Output[1] 19Examplex9118])Output[1] 11Examplex105000])Output[1] 68
Read MoreHow to perform homogeneity of variance test for two-way anova in R?
In general, we can say that the homogeneity of variance test is the type of test that compares the variance of two or more variables and finds the significant difference between or among them if exists. For a two-way anova, one of the most commonly used homogeneity of variance test is Levene’s Test and it can be easily done with the help of leveneTest function of car package in base R.Consider the below data frame −Exampleset.seed(151) x1F) group 6 0.6593 0.6835 13
Read MoreHow to count the number of occurrences of all unique values in an R data frame?
A data frame in R can have infinite number of unique values and it can also contain many repeated values. Therefore, finding the number of all unique values in the data frame can help us to understand the diversity in the data but this most done in situations where we expect to have repeated elements otherwise it would not make sense. To count the number of occurrences of all unique values, we can use table function along with the unlist as shown in the below examples.Consider the below data frame −Examplex1
Read MoreHow to generate Bernoulli random variable in R?
Each value in Bernoulli random variable represents success or a failure for a single trial that makes it different from Binomial random variable because a Binomial random variable represents number of success or failure for a number of trials. To generate a Bernoulli random variable, we can use rbinom function but we need to pass 1 for size argument.Examplerbinom(120,1,0.71)Output[1] 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 1 1 1 1 0 1 [38] 1 0 1 1 1 0 0 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 0 [75] 1 1 0 1 1 1 1 1 0 0 1 0 1 1 1 0 1 1 1 1 1 1 0 1 1 1 0 1 1 0 1 1 1 1 1 1 0 [112] 0 0 1 1 0 1 1 1 1Examplerbinom(120,1,0.1)Output[1] 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 1 0 [38] 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [75] 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0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0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [75] 1 0 0 1 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 1 0 0 0 1 [112] 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 [149] 0 0 0 0 1 0 0 0 0 1 0 1 0 0 0 1 0 0 0 1 1 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 [186] 0 1 1 0 0 0 0 0 0 1 0 0 0 0 1Examplerbinom(200,1,0.35)Output[1] 0 1 0 0 0 0 1 0 0 1 0 1 0 0 0 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 [38] 1 0 1 1 0 0 1 0 0 1 1 1 0 1 1 0 1 1 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1 0 0 1 1 [75] 1 1 0 0 0 1 0 0 1 0 0 0 0 0 1 0 1 0 1 1 0 0 1 1 1 0 0 0 0 1 0 0 1 0 1 0 0 [112] 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 1 1 0 1 1 1 1 0 0 1 0 1 1 0 0 0 0 1 1 [149] 1 0 0 1 1 0 1 0 0 0 1 1 0 1 1 0 1 0 1 1 0 0 0 0 0 0 1 0 0 1 0 1 0 1 0 0 0 [186] 0 1 0 0 0 1 0 0 1 1 0 1 0 0 0
Read MoreHow to make all the elements in a list of equal size in R?
We know that a list can multiple elements of different types as well as of different size. For example, a list that contains two elements then one element may contain fifteen elements and the other might have twenty-five elements. In this situation, we might want to fill the first element with ten more elements so that the size of both the elements become equal. This can be done by using lapply function as shown in the below examples.Consider the below list −Exampleset.seed(101) x1
Read More