Server Side Programming Articles - Page 1486 of 2646

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

Nizamuddin Siddiqui
Updated on 06-Nov-2020 13:23:03

234 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 4Example1 Live Demofor (i in 1:2) for (j in 1:2) cat(i, j, i*j, "") Output1 1 1 1 2 2 2 1 2 2 2 4Example2 Live Demofor (i in 1:5) for (j in 1:5) cat(i, j, i*j, "") ... Read More

How to convert MANOVA data frame for two-dependent variables into a count table in R?

Nizamuddin Siddiqui
Updated on 06-Nov-2020 13:21:54

263 Views

MANOVA refers to multivariate analysis of variance, in this method we have more than one dependent variable and multiple independent variables. We want to compare each level of the independent variable combination for each of the dependent variables. To convert MANOVA data frame for two-dependent variables into a count table, we can use cast function of reshape package but we need to melt the data frame first so that the casting can be done appropriately.Example Live DemoConsider the below data frame −Gender

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

Nizamuddin Siddiqui
Updated on 06-Nov-2020 13:06:51

222 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

How to create a random sample of months in R?

Nizamuddin Siddiqui
Updated on 06-Nov-2020 13:03:02

438 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.Examples Live Demox1

How to subset a data frame by excluding a specific text value in an R data frame?

Nizamuddin Siddiqui
Updated on 06-Nov-2020 13:02:02

873 Views

To create a subset based on text value we can use rowSums function by defining the sums for the text equal to zero, this will help us to drop all the rows that contains that specific text value. For example, if we have a data frame df that contains A in many columns then all the rows of df excluding A can be selected as−df[rowSums(df=="A")==0,,drop=FALSE]Example Live DemoConsider the below data frame −set.seed(951) x1

How to highlight text inside a plot created by ggplot2 using a box in R?

Nizamuddin Siddiqui
Updated on 06-Nov-2020 12:56:07

275 Views

There might be many ways to highlight text inside a plot but the easiest one would be using geom_label function of ggplot2 package, with the help of this function we can put the required text and the aesthetics of that text by using a single line of code. It is highly recommended that we should use geom_label function with desired specifications.Example Live DemoConsider the below data frame −set.seed(222) x

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

Nizamuddin Siddiqui
Updated on 06-Nov-2020 12:48:06

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.Example1Live Demo> 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 ... Read More

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

Nizamuddin Siddiqui
Updated on 06-Nov-2020 12:45:58

320 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.Example1Live Demo> 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 ... Read More

How to remove NA’s from an R data frame that contains them at different places?

Nizamuddin Siddiqui
Updated on 06-Nov-2020 12:23:26

311 Views

If NA values are placed at different positions in an R data frame then they cannot be easily removed in base R, we would be needing a package for that. The best package to solve this problem is dplyr and we can use summarise_each function of dplyr with na.omit to remove all the NA’s. But if we have more than one column in the data frame then the number of non-NA values must be same in all the columns.ExampleConsider the below data frame:Live Demo> x1 x2 df1 df1Output x1 x2 1 NA 15 2 NA 15 3 NA 15 ... Read More

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

Nizamuddin Siddiqui
Updated on 06-Nov-2020 12:13:04

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:Live Demo> set.seed(3654) > x1 x2 x3 x4 df1 df1Output x1 x2 x3 x4 1 4 5 16 2 2 5 4 ... Read More

Advertisements