Found 2038 Articles for R Programming

How to select columns in R without missing values?

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

2K+ 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 method will be used as t(na.omit(t(df))).ExampleConsider the below data frame − Live Demodf1

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

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

494 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 scale_x_continuous(sec.axis=dup_axis()) and scale_y_continuous(sec.axis=dup_axis()). Check out the below example to understand how it can be done.ExampleConsider the below data frame − Live Demox

How to divide each column by a particular column in R?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 11:02:17

12K+ Views

To divide each column by a particular column, we can use division sign (/). For example, if we have a data frame called df that contains three columns say x, y, and z then we can divide all the columns by column z using the command df/df[,3].ExampleConsider the below data frame − Live Demox1

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

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

507 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 score for this data −ave(df$Response,df$GROUP,FUN=scale)ExampleConsider the below data frame − Live Demogrp

How to find the correlation matrix with p-values for an R data frame?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 10:51:59

9K+ Views

The correlation matrix with p-values for an R data frame can be found by using the function rcorr of Hmisc package and read the output as matrix. For example, if we have a data frame called df then the correlation matrix with p-values can be found by using rcorr(as.matrix(df)).ExampleConsider the below data frame − Live Demodf1

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

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 Demox

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

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

737 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,2)ExampleConsider the below matrix − Live DemoM1

How to change the tick size using ggplot2 in R?

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

2K+ 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 using the below command −ggplot(df,aes(x,y))+geom_point()+theme(axis.ticks.length=unit(0.8,"inch"))ExampleConsider the below data frame − Live Demox

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

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

614 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 character column. Check out the below example to understand how it can be done.ExampleConsider the below data frame df1 and the vector v1 − Live Demodf1

How to convert number to words in an R data frame column?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 10:21:07

3K+ Views

To convert number to words in an R data frame column, we can use english function from english package. For example, if we have a data frame called df that contains a number column x then we can convert the numbers into words by using the command as.character(english(df$x)).ExampleConsider the below data frame − Live Demox

Advertisements