Found 2038 Articles for R Programming

How to find the index of the last occurrence of repeated values in a vector in R?

Nizamuddin Siddiqui
Updated on 10-Oct-2020 12:41:25

463 Views

Indexing helps us to understand the location of the value in the vector. If we have a vector that contains repeated values then we might want to figure out the last occurrence of the repeated value. For example, if we have a vector x that contains 1, 1, 2, 1, 2 then the last occurrence of repeated values will be 4 and 5 because the last 1 is at 4th position and 2 is at the 5th position. We can find this by using tapply function in R.Example Live Demox1

How to join points in a point chart with lines using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 10-Oct-2020 12:35:08

405 Views

Usually, a point chart is created to assess the relationship or movement of two variables together but sometimes these points are scattered in a way that makes confusion. Hence, data analyst or researcher try to visualize this type of graph by joining the points with lines. In ggplot2, this joining can be done by using geom_line() function.Consider the below data frame −Example Live Demoset.seed(111) x

How to generate a normal random vector using the mean of a vector in R?

Nizamuddin Siddiqui
Updated on 10-Oct-2020 12:32:07

750 Views

To create a normal random vector, we can use rnorm function with mean and standard deviation as well as without passing these arguments. If we have a different vector derived from another distribution or simply represent some numbers then we can use the mean of that vector in the rnorm function for mean argument.Example Live Demoset.seed(101) x1

How to find prime numbers between two values or up to a value in R?

Nizamuddin Siddiqui
Updated on 10-Oct-2020 12:30:22

911 Views

We know that the prime numbers are the numbers that are not divisible by any number except by themselves or 1 and 1 is not considered as a prime number. In R, we can find the prime numbers up to a number or between two numbers by using Prime function of numbers package. If we want to find the total number of prime numbers between 1 and 10, then we just need to pass 10 inside the Prime function, otherwise range will be required.Loading numbers package −library("numbers")Primes(10)[1] 2 3 5 7Primes(100)[1] 2 3 5 7 11 13 17 19 23 ... Read More

How to find the difference between regression line and the points in R?

Nizamuddin Siddiqui
Updated on 10-Oct-2020 12:28:47

214 Views

The difference between regression line and the points on the scatterplot are actually the residuals, thus we need to calculate the residual from the model object. This can be simply done by using residuals function. For example, if we create a linear model defined as Model between x and y then the residuals will be found as residuals(Model).Consider the below data frame −Example Live Demoset.seed(999) x1

How to convert rows in an R data frame to list?

Nizamuddin Siddiqui
Updated on 10-Oct-2020 12:18:07

1K+ Views

Sometimes each row needs to be treated differently, therefore, we might want to convert those rows into list. This will help us to perform the operations on our row elements separately. To convert the rows into list, we can use split function by defining the number of rows in the data frame.Consider the below data frame −Example Live Demoset.seed(101) x1

How to combine two matrices to create a block-diagonal matrix in R?

Nizamuddin Siddiqui
Updated on 10-Oct-2020 12:10:13

627 Views

A block-diagonal matrix means that a matrix added to another matrix at the end the last element. For example, if we have a matrix with nine values and the other matrix also has nine values then the second matrix will be added to the first matrix and the elements below first matrix will be zero and the elements above the second matrix will also be zero.Example Live DemoM1

How to find the cube root of negative values in R?

Nizamuddin Siddiqui
Updated on 10-Oct-2020 11:55:32

577 Views

There is no function in R to find the cube root of negative values, hence we need to create that. The code to create the function is as shown below −CubeRoot

How to create side-by-side boxplot in base R?

Nizamuddin Siddiqui
Updated on 10-Oct-2020 11:54:02

314 Views

Often, we need to compare continuous variables using boxplots and thus side-by-side boxplots are required. Creating side-by-side boxplot in base R can be done with the help of creating space for graphs with the help of par(mfrow=). In this function, we can define the number of graphs and the sequence of these graphs, thus creation of side-by-side boxplot will become easy.Consider the below vectors −set.seed(100) x

How to create a matrix using vector of string values in R?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 15:40:50

2K+ Views

You might have heard that matrix can contain only numerical values but it is also possible to create a matrix with string values, and of course calculations using these types of matrices would not be possible. To create a matrix using string values, we can first create a vector of strings then define its dimension with dim function and that will convert the vector into matrix of string values.Example Live DemoM1

Advertisements