Found 33676 Articles for Programming

How to create a random sample of months in R?

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

405 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

817 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

251 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

2K+ 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

292 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

276 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

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

Nizamuddin Siddiqui
Updated on 06-Nov-2020 12:09:17

320 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:Live Demo> set.seed(14) > x y df dfOutput x y 1 1 0.6690751 2 2 1.8594771 3 ... Read More

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

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

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 set the alignment of labels in horizontal bar plot to left in R?

Nizamuddin Siddiqui
Updated on 06-Nov-2020 12:00:15

1K+ Views

When we create a horizontal bar plot using ggplot2 package, the labels of the categorical variable are aligned to the right-side of the axis and if the size of these labels are different then it looks a little ambiguous. Therefore, we might want to set the alignment of the labels to left-side and this can be done by using theme function of ggplot2 package.ExampleConsider the below data frame:> df dfOutput    x y 1 India  14 2 UK     15 3 Russia 12 4 United States of America 18Loading ggplot2 package and creating a horizontal ... Read More

Advertisements