Nizamuddin Siddiqui has Published 1958 Articles

How to select columns in R without missing values?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 16-Mar-2021 11:09:52

3K+ Views

There are two easy methods to select columns of an R data frame without missing values, first one results in a vector and other returns a matrix. For example, if we have a data frame called df then the first method can be used as df[, colSums(is.na(df))==0] and the second ... Read More

How to display tick marks on upper as well as right side of the plot using ggplot2 in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 16-Mar-2021 11:05:36

827 Views

To display tick marks on upper as well as right side of the plot, we can create duplicate axes for X as well Y by using scale_x_continuous and scale_y_continuous functions. The argument that will help us in this case is sec.axis and we need to set it to dup_axis as ... Read More

How to calculate the z score for grouped data in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 16-Mar-2021 10:56:35

691 Views

To calculate the z score for grouped data, we can use ave function and scale function. For example, if we have a data frame called df that contains a grouping coloumn say GROUP and a numerical column say Response then we can use the below command to calculate the z ... Read More

How to create horizontal lines for each bar in a bar plot of base R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 16-Mar-2021 10:44:37

2K+ Views

To create horizontal lines for each bar in a bar plot of base R, we can use abline function and pass the same values as in the original barplot with h argument that represents horizontal with different color to make the plot a little better in terms of visualization.Example Live DemoxRead More

How to convert the row values in a matrix to row percentage in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 16-Mar-2021 10:38:16

950 Views

To convert the row values in a matrix to row percentage, we can find the row sums and divide each row value by this sum. For example, if we have a matrix called M then we can convert the row values in M to row percentage by using the commandround((M/rowSums(M))*100, ... Read More

How to change the tick size using ggplot2 in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 16-Mar-2021 10:31:50

3K+ Views

To change the tick size using ggplot2, we can use theme function with argument axis.ticks.length. For example, if we have a data frame called df that contains two columns say x and y then the scatterplot between x and y with larger size of tick marks can be created by ... Read More

How to combine a data frame and a named vector if name matches with a column in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 16-Mar-2021 10:28:47

928 Views

If we have a data frame that contains a character column and a named vector which has the same names as in the character column of the data frame then we can combine this data frame and the vector by using match function be appropriately defining the names and the ... Read More

How to convert negative values in a matrix to 0 in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 16-Mar-2021 10:10:24

5K+ Views

To convert negative values in a matrix to 0, we can use pmax function. For example, if we have a matrix called M that contains some negative and some positive and zero values then the negative values in M can be converted to 0 by using the command pmax(M, 0).ExampleConsider ... Read More

How to minus one column from another in an R matrix?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 16-Mar-2021 07:44:09

6K+ Views

To minus one column from another in an R matrix, we first need to read the matrix as a data frame using as.data.frame then find minus the columns using minus sign and accessing the column of the data frame. To understand how it can be done look at the steps ... Read More

How to sort each row of an R data frame in increasing order?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 16-Mar-2021 07:35:06

2K+ Views

To sort each row of an R data frame in increasing order, we can use apply function for sorting the columns and then transpose the output. For example, if we have a data frame called df that contains 5 columns then each row of df can be sorted in increasing ... Read More

Advertisements