Nizamuddin Siddiqui

Nizamuddin Siddiqui

1,958 Articles Published

Articles by Nizamuddin Siddiqui

Page 59 of 196

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 326 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 create a random sample of months in R?

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

Random samples can be created by using sample function and months in a year can be generated with the help of month.name function. Therefore, if we want to create a random sample of months then month.name can be used with sample function as sample(month.name) and if the size of the sample is larger than 12 then replace=TRUE argument should be used.Examplesx1

Read More

How to replace “and” in a string with “&” in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 238 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

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 243 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 create a dendrogram without X-axis labels in R?

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

A dendrogram display the hierarchical relationship between objects and it is created by using hierarchical clustering. In base R, we can use hclust function to create the clusters and the plot function can be used to create the dendrogram. For example, if we want to create the dendrogram for mtcars data without X−axis labels then it can be done as shown below −hc=hclust(dist(mtcars)) plot(hc, xlab="", sub="")Examplehead(mtcars) mpg 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 Wag 21.0 6 160 110 3.90 2.875 17.02 ...

Read More

How to create a dashed line that passes through Y = 1 in base R?

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

Usually, plots are created with solid lines but sometimes we need to use dashed line so that the points can represent a threshold or something critical, the main objective here is to get the attention of the reader on these points. In base R, the plots are created with plot function and we can use abline function with lty =2 to draw dashed lines.Example1x

Read More

How to multiply a matrix columns and rows with the same matrix rows and columns in R?

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

To multiply a rows or columns of a matrix, we need to use %*% symbol that perform the multiplication for matrices in R. If we have a matrix M with 5 rows and 5 columns then row 1 of M can be multiplied with column 1 of M using M[1,]%*%M[,1], similarly, we can multiply other rows and columns.ExampleM

Read More

How to find the row-wise index of non-NA values in a matrix in R?

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

A matrix can also contain missing values and those missing values can be placed in a matrix by randomization as well, hence we cannot be sure about the positions of those values that are referred to as NA and the non-missing values. If we want to find the positions of the non-missing values in a matrix then apply function can be used where we can use which function to exclude NA values. Check out the below examples to understand how it works.Example1M1

Read More
Showing 581–590 of 1,958 articles
« Prev 1 57 58 59 60 61 196 Next »
Advertisements