R Programming Articles

Page 57 of 174

How to create different Y-axis for group levels using ggplot2 in R?

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

If we have a categorical variable or a group variable then we might want to create a line chart for each of the categories or levels, this will help us to understand the range of multiple levels in a single plot. For this purpose, we can use facet_grid function of ggplot2 package as shown in the below example.ExampleConsider the below data frame:> x y df dfOutput x y 1 C -1.55668689 2 A 2.41399136 3 D -0.78520253 4 A -0.43092594 5 C 1.94379390 6 A ...

Read More

How to draw a circle in R?

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

There is no direct function in R to draw a circle but we can make use of plotrix package for this purpose. The plotrix package has a function called draw.cirlce which is can be used to draw a circle but we first need to draw a plot in base R then pass the correct arguments in draw.circle. The first and second arguments of draw.circle takes x and y coordinates, and the third one is for radius, hence these should be properly chosen based on the chart in base R.Loading plotrix package:> library(plotrix)Creating different circles using draw.circle:Example> plot(1:10, type="n") > draw.circle(2, ...

Read More

How to convert diagonal elements of a matrix in R into missing values?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 985 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 missing values/NA by using diag function.Example1> M1 M1Output  [, 1] [, 2] [, 3] [, 4] [1, ] 1   5    9   13 [2, ] 2   6   ...

Read More

How to create a new column in an R data frame based on some condition of another column?

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

Sometimes we want to change a column or create a new by using other columns of a data frame in R, this is mostly required when we want to create a categorical column but it can be done for numerical columns as well. For example, we might want to create a column based on salary for which if salaries are greater than the salary in another column then adding those salaries otherwise taking the difference between them. This will help us to understand whether the salaries in two columns are equivalent, lesser, or greater. In R, we can use transform ...

Read More

How to create line chart using ggplot2 in R with 3-sigma limits?

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

To create a line chart with 3-sigma limits using ggplot2, we first need to calculate the limits then the chart can be created. We can use geom_ribbon function of ggplot2 for this purpose where we can pass lower 3-sigma limit for ymin argument in aes and upper 3-sigma limit for ymin argument in aes, also we need to specify alpha so that the color of lines and the limits can be differentiated.ExampleConsider the below data frame:> set.seed(14) > x y df dfOutput x y 1 1 0.6690751 2 2 1.8594771 3 3 ...

Read More

How to check if a value exists in an R data frame or not?

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

There are many small objectives that helps us to achieve a greater objective in data analysis. One such small objective is checking if a value exists in the data set or not. In R, we have many objects for data set such as data frame, matrix, data.table object etc. If we want to check if a value exists in an R data frame then any function can be used.ExampleConsider the below data frame:> set.seed(3654) > x1 x2 x3 x4 df1 df1Output x1 x2 x3 x4 1 4 5 16 2 2 5 4 15 ...

Read More

How to create a contingency table using datasets in base R?

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

A contingency table is a cross-tabulation that looks like a matrix. These tables can have different as well as equal number of columns and rows. If we want to create a contingency table using datasets in base R then table function can be used. For example, if we want to create a contingency table for cyl and gear column of mtcars data then it can be done as shown in the below example 1.Example1> head(mtcars)Outputmpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 ...

Read More

How to calculate the sensitivity and specificity from a confusion matrix in R?

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

If we have a confusion matrix then the sensitivity and specificity can be calculated using confusionMatrix function of caret package. For example, if we have a contingency table named as table then we can use the code confusionMatrix(table). This will return sensitivity and specificity as well as many other metrics.Example1> x1 y1 table1 table1Outputy1 x1 a b c d a 0 0 1 0 b 0 1 2 1 c 2 2 0 2 d 3 2 1 3Loading caret package:> library(caret)Finding sensitivity and specificity of table1:> confusionMatrix(table1)Confusion Matrix and StatisticsOutputy1 x1 a b c d ...

Read More

How to find the combination of multiplication of integers up to a certain value in R?

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

Suppose we want to find the total number of combinations of two numbers, say, 1 and 2 and then multiply each of the combination values. This will result in the following combinations −1 1 1 2 2 1 2 2And the multiplication will also have a third column as shown below −Multiplication 1 1 1 1 2 2 2 1 2 2 2 4Example1for (i in 1:5) for (j in 1:3) cat(i,j,i*j, "") Output1 1 1 1 2 2 1 3 3 2 1 2 2 2 4 2 3 6 3 1 3 3 2 6 3 3 9 4 1 4 4 2 8 4 3 12 5 1 5 5 2 10 5 3 15Example4for (i in 1:4) for (j in 1:6) cat(i,j,i*j, "") Output1 1 1 1 2 2 1 3 3 1 4 4 1 5 5 1 6 6 2 1 2 2 2 4 2 3 6 2 4 8 2 5 10 2 6 12 3 1 3 3 2 6 3 3 9 3 4 12 3 5 15 3 6 18 4 1 4 4 2 8 4 3 12 4 4 16 4 5 20 4 6 24

Read More

How to replace "and" in a string with "&" in R?

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

We know that the word “and” can be written as “&”. If we have vectors that contain string values separated with word “and” then we can replace it with “&”. To do this, we can use stri_replace_last function of stringi package. For example, if we have a string vector that contain only one element defined as x

Read More
Showing 561–570 of 1,740 articles
« Prev 1 55 56 57 58 59 174 Next »
Advertisements