R Programming Articles

Page 56 of 174

How to find the intersection between two or more lists in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 9K+ Views

The intersection of lists means the elements that are unique and common between the lists. For example, if we have a list that contains 1, 2, 3, 3, 3, 2, 1 and the other list that contains 2, 2, 1, 2, 1 then the intersection will return only those elements that are common between the lists and also unique, hence for this example we will get 1 and 2. In R, we can do this by using intersection function along with Reduce function.Consider the below lists −ExampleList1

Read More

How to exclude extra margin between points and the axes for a plot created by using ggplot2 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 1K+ Views

In a plot created by using ggplot package there exists an extra area around all the sides of the plot which uses extra space, thus we might want to get rid of that space by removing that extra margin area. It can be done by setting the scale for both the axes to zero with the help of scale_x_continuous and scale_y_continuous function.Consider the below data frame −Exampleset.seed(151) x

Read More

How to remove multiple rows from an R data frame using dplyr package?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 611 Views

Sometimes we get unnecessary information in our data set that needs to be removed, this information could be a single case, multiple cases, whole variable or any other thing that is not helpful in achieving our analytical objective, hence we want to remove it. If we want to remove such type of rows from an R data frame with the help of dplyr package then anti_join function can be used.ExampleConsider the below data frame:> set.seed(2514) > x1 x2 df1 df1Output x1 x2 1 5.567262 4.998607 2 5.343063 4.931962 3 2.211267 5.034461 ...

Read More

How to create a barplot with gaps on Y-axis scale in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 743 Views

If we want to have gaps on Y-axis scale in a barplot then it cannot be done in base R. For this purpose, we can make use of gap.barplot function of plotrix package. The gap.barplot function is very handy, we just need to pass the vector for which we want to create the barplot and the gap values simply by using gap argument.Loading plotrix package:> library(plotrix)Example1> x xOutput[1] 2 6 5 4 7 2 5 2 5 2 8 6 8 13 3 5 7 7 5 6> gap.barplot(x, gap=c(2, 4)) ylim 0 11Warning message:In gap.barplot(x, gap = c(2, 4)) ...

Read More

How to create normal quantile-quantile plot for a logarithmic model in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 323 Views

How to create normal quantile-quantile plot for a logarithmic model in R?A logarithmic model is the type of model in which we take the log of the dependent variable and then create the linear model in R. If we want to create the normal quantile-quantile plot for a logarithmic model then plot function can be used with the model object name and which = 2 argument must be introduced to get the desired plot.Example1> x1 x1Output[1]  4.735737 3.631521 5.522580 5.538314 5.580952 4.341072 4.736899 2.455681 [9]  4.042295 5.534034 4.717607 6.146558 4.466849 5.444437 5.390151 4.491595 [17] 4.227620 4.223362 5.452378 5.690660 5.321716 5.269895 ...

Read More

How to set the diagonal elements of a matrix to 1 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 1K+ Views

First thing we need to understand is diagonal elements are useful only if we have a square matrix, otherwise it would not make sense to set diagonal elements, this is known to almost all mathematicians but some freshman might get confused because we can create diagonal in a non-square matrix which should not be called a diagonal. In R, we can set the diagonal elements of a matrix to 1 by using diag function.Example1> M1 M1Output     [, 1] [, 2] [, 3] [, 4] [, 5] [1, ]   1    6   11   16   21 [2, ...

Read More

How to fix the coefficient of an independent variable in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 712 Views

While doing the analysis we might know the variation in an independent variable or we might want to understand how other independent variables behave if we fix a certain variable. Therefore, we can fix the coefficient of an independent variable while creating the model and this can be done by using offset function with the coefficient of the variable for which we want to fix the value of the coefficient.ExampleConsider the below data frame:> set.seed(854) > x1 x2 y1 df1 df1Output x1 x2 y1 ...

Read More

How to create a point chart with point size increment based on the position of the point in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 348 Views

The cex argument is used to increase or decrease the point size in a point chart created in base R. If we want to create a point chart with points of size in increment manner then we can pass a vector of the same size as the vector for which we want to create the point chart. For example, if we have a vector x that contains 10 elements then cex will be set as cex=1:10.Example1> x plot(x, cex=1:10, xlim=c(1, 12), ylim=c(-2, 12))Output:ExampleLet’s have a look at another example:> y plot(y, cex=1:10, xlim=c(1, 12), ylim=c(-1, 12))Output:

Read More

How to change the resolution of a plot in base R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 3K+ Views

In base R, we can save a plot as a png and pass the resolution in the same stage. The procedure to do this is creating the png image with resolution with res argument then creating the plot and using dev.off() to create the file. Check out the below examples to understand how it works.Example1> png(file="example1.png",res=100) > plot(1:10) > dev.off()Output:Example2> png(file="example2.png",res=200) > plot(1:10) > dev.off()Output

Read More

How to change legend values in a bar plot created by using ggplot2 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 768 Views

How to change legend values in a bar plot created by using ggplot2 in R?By default, the legend values are taken as the different levels of the categorical variable for which a bar plot is created using ggplot2 package and if we want to change those values then scale_color_manual function of the ggplot2 package can be used where we need to pass the values for color and labels for legend values.ExampleConsider the below data frame:> set.seed(1214) > x1 y1 df1 df1Output x1 y1 1 B 4 2 B 5 3 C 5 4 ...

Read More
Showing 551–560 of 1,740 articles
« Prev 1 54 55 56 57 58 174 Next »
Advertisements