Found 2038 Articles for R Programming

How to change ordinal X-axis label to text labels using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 11:01:17

1K+ Views

A plot created with ordinal values on X-axis needs to be ordered for plotting, otherwise, the plot will have continuous values on the X-axis that includes ordinal values. If we want to convert those values to text then scale_x_discrete should be used with the number of breaks, these number of breaks are the actual number of labels we want to use in our plot.ExampleConsider the below data frame −x

How to check if two data frames same or not in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 10:46:51

579 Views

Two data frames can be same if the column names, row names and all the values in the data frame are exactly same. We might to check this for data frames that we expect to be same, for example, if we have two data sets each one of have same number of rows, same number of columns, same data type for each of the columns, and the data view shows that values are same then it is worth checking whether the complete data sets are same or not. To do this checking in R, we can use identical function.Examples Live Demodf1Read More

How to create a line chart using ggplot2 with a vertical line in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 10:40:52

191 Views

In general, the line chart is drawn to view the trend of something and we might also have some threshold point for that trend, for example, if blood pressure is plotted then we might want to show 60 mm Hg as well because this is the lowest acceptable value for blood pressure recommended by doctors. Therefore, it can be plotted as a vertical line if we want to plot blood pressures of a person. Similarly, there can be many situations where we can use a vertical line to visualize the threshold value. This can be achieved in ggplot2 with the ... Read More

How to find the mean of columns of an R data frame or a matrix?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 10:38:38

1K+ Views

If all the columns in an R data frame are numeric then it makes sense to find the mean for each of the columns. This calculation will help us to view how different the values of means are for each of the columns but to make sure that they are significantly different, we will need to run a hypothesis test. To find the column means of a data frame or a matrix we can use colMeans function.ExampleConsider the below data frame − Live Demoset.seed(9) x1

How to extract the p-value and F-statistic from aov output in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 10:35:11

4K+ Views

The analysis of variance technique helps us to identify whether there exists a significant mean difference in more than two variables or not. To detect this difference, we either use F-statistic value or p-value. If the F-statistic value is greater than the critical value of F or if p-value is less than the level of significance then we say that at least one of the means is significantly different from the rest. To extract the p-value and F-statistic value, we can make use of summary function of the ANOVA model.Example Live Demoset.seed(123) Group

How to collapse factor levels in an R data frame?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 10:33:36

556 Views

Sometimes the levels of a factor are not correctly recorded, for example, recording male with M in some places and with Mal in some places hence there are two levels for level male. Therefore, the number of levels increases if the factor levels are incorrectly recorded and we need to fix this issue because the analysis using these factor levels will be wrong. To convert the incorrect factor levels into the appropriate ones, we can use list function to define those levels.Example 1 Live DemoF

How to convert two columns of an R data frame to a named vector?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 06:55:29

342 Views

If two columns are of a form such that one column contains the name of the vector values and another column having the values of a vector then we might want to convert them into a vector. To do this, we can simply read the vectors with their data type and structure them with structure function.Example 1 Live Demox1

How to find the maximum using aggregate and get the output with all the columns in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 06:53:39

418 Views

When we use aggregate function to find maximum or any other value, the output of the aggregation does not provide all the columns that corresponds to the maximum value. Therefore, we need to merge the data frame obtained by using aggregate with the original data frame. In this way, we will get only those rows that are common between the new data frame and the original one.ExampleConsider the below data frame − Live Demoset.seed(99) x1

How to join the points of a point chart if X-axis values are categorical in R using ggplot2?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 06:51:42

525 Views

A point chart is usually drawn to see the relationship between two continuous variables and it is also called scatterplot but if the independent variable is categorical then we simply call it a point chart. Often, we want to join or connect the points of a point chart to visually represent the variation of categories of the independent variable and make it a line chart. This can be done by setting stat_summary argument geom to line and setting group = 1 in aes.ExampleConsider the below data frame − Live DemoClass

How to remove NULL values from a list in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 06:45:44

5K+ Views

The value NULL is used to represent an object especially a list of length zero. If a list contains NULL then we might want to replace it with another value or remove it from the list if we do not have any replacement for it. To remove the NULL value from a list, we can use the negation of sapply with is.NULL.Examples Live Demox

Advertisements