Nizamuddin Siddiqui has Published 2307 Articles

How to find the mean for multiple columns in an R data frame using mean function not colMeans?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 04-Mar-2021 19:15:04

248 Views

The easiest way to find the column means of an R data frame is to use colMeans function but if we do not want to use it then it can be done with the help of sapply. While using sapply, we need to make sure that we are selecting only ... Read More

How to remove rows in a data.table object with NA's in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 04-Mar-2021 18:29:49

2K+ Views

If a row contains missing values then their sum will not finite, therefore, we can use is.finite function with the data.table object to remove the rows with NA’s. For example, if we have a data.table object called DT that contains some rows with NA’s then the removal of those rows ... Read More

How to create boxplot with multiple factor levels using ggplot2 in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 04-Mar-2021 18:23:00

4K+ Views

To create a boxplot, we have one factor and one numerical column and the boxplot is created for each category or levels in that factor. Now if we have two factors then the boxplot can be created for both factor levels by passing fill argument in geom_boxplot. This will help ... Read More

How to create a histogram with dots instead of bars in base R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 04-Mar-2021 08:56:51

510 Views

To create a histogram with dots instead of bars, we can use the plot function in base R. The x values will be based on the sorting of the vector values and the y values will be based on the sequence of the table for the vector values. Therefore, we ... Read More

How to subset a data frame based on a vector values in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 04-Mar-2021 08:55:30

3K+ Views

If we have a vector and a data frame, and the data frame has a column that contains the values similar as in the vector then we can create a subset of the data frame based on that vector. This can be done with the help of single square brackets ... Read More

How to display average line for y variable using ggplot2 in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 04-Mar-2021 08:54:08

2K+ Views

To display the average line for y variable using ggplot2, we can use geom_hline function along with the yintercept. In the yintercept, we would need to calculate the mean of the y variable and we can also change the colour of the line using color argument inside the geom_hline function.ExampleConsider ... Read More

How to create matrix with random integer values in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 04-Mar-2021 08:53:04

524 Views

To create a vector of random integers we can use the function sample.int and if we want to create the matrix of such integers then matrix function will be used along with it. For example, if we want to create a matrix with random integers between 1 to 100 of ... Read More

How to convert NaN values to NA in an R data frame?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 04-Mar-2021 08:52:10

500 Views

To convert NaN values to NA, we would need to detect the NaN values in the data frame and the set them to NA. For example, if we have a data frame called df that contains a column x which has some NaN values then we can convert those NaN ... Read More

How to highlight a bar in base R histogram?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 04-Mar-2021 08:41:48

421 Views

To highlight a bar in base R histogram, we need to understand the X-axis values and pass the col argument inside hist function appropriately. We just need to put a separate value for the bar that we want to highlight and set the colouring of the rest of the bars ... Read More

How to convert a binary matrix to logical matrix in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 04-Mar-2021 08:33:27

750 Views

A binary matrix contains values such as Yes or NO, 1 or 0, or any other two values that represents opposite mostly and the globally accepted logical values are FALSE and TRUE. Therefore, to convert a binary matrix to logical matrix, we can use ifelse function and convert the one ... Read More

Advertisements