Sort Matrix Columns Independently in Increasing Order in R

Nizamuddin Siddiqui
Updated on 06-Feb-2021 10:11:41

469 Views

To sort a matrix column independently in increasing order means that ordering each column of the data frame in increasing order, therefore, sorting of values in one column does not affect the sorting in other columns. This can be done with the help of apply function along with the sort function as shown in the below examples.Example Live DemoM1

Remove Y-Axis Labels in R

Nizamuddin Siddiqui
Updated on 06-Feb-2021 09:16:32

14K+ Views

When we create a plot in R, the Y-axis labels are automatically generated and if we want to remove those labels, the plot function can help us. For this purpose, we need to set ylab argument of plot function to blank as ylab="" and yaxt="n" to remove the axis title. This is a method of base R only, not with ggplot2 package.Examplex

Remove Single Quote from String Column in R Data Frame

Nizamuddin Siddiqui
Updated on 06-Feb-2021 09:13:50

5K+ Views

Sometimes column values in an R data frame have single quote associated with them and to perform the analysis we need to remove that quote. Therefore, to remove single quote from string column, we can use gsub function by defining the single quote and replacing it with blank(not space) as shown in the below examples.Consider the below data frame −Example Live Demox1

Extract First Value from a List in R

Nizamuddin Siddiqui
Updated on 06-Feb-2021 09:09:46

6K+ Views

To extract first value from a list, we first need to access the element using double square brackets then the sub-element of each element will be accessed using single square brackets. For example, if we have a list called LIST containing five elements each having 10 elements then the first sub-element of the LIST will be selected by using LIST[[1]][1].Example Live DemoList1

Put Plot Title Inside the Plot Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 06-Feb-2021 09:08:10

871 Views

Generally, we put the plot title on top side of the plot but we can put it inside the plot as well. Of course, this will change the display of the chart but it will also get attraction of viewers. To do this, we can use the theme function of ggplot2 package where margin argument for plot title will change the position of the title.Consider the below data frame −Example Live Demox

Find Column Index in R Data Frame that Matches a Condition

Nizamuddin Siddiqui
Updated on 06-Feb-2021 09:05:21

1K+ Views

To find the column index that matches a condition, we can use apply function. This condition could be values in columns greater than something, less than something, equal to something, or any other condition for numerical variables. For example, if we want to check which columns of data df contains value in rows greater than 5 then we can use the command apply(df,1, function(x) which(x>5)).Consider the below data frame −Example Live Demox1

Sort R Data Frame Column Without Losing Row Names

Nizamuddin Siddiqui
Updated on 06-Feb-2021 09:03:03

1K+ Views

When we sort a data frame column in R, the row names are lost but we might need them. Therefore, sorting without losing row names is required and it can be done with the help of order function. For example, if we have a data frame called df that has a column x then sorting of x without losing row names can be done by using the below command −df[order(df$x),,drop=FALSE]Consider the below data frame −Example Live Demox1

Check If Two Matrices Are Equal in R

Nizamuddin Siddiqui
Updated on 06-Feb-2021 08:58:00

2K+ Views

When we matrices of larger size and the data is expected to from the same distribution or from same sources then we might expect that the matrices are equal. In this type of situations, we would like to check whether the two matrices are equal or not. This can be done with the help of all.equal function as shown in the below examples.Example Live DemoM1

Display 0 at Y Axis using ggplot2 in R

Nizamuddin Siddiqui
Updated on 06-Feb-2021 08:48:33

1K+ Views

To display 0 at Y-axis, we can set the limits for Y-axis using scale_y_continuous function of ggplot2 package. For example, if we have two columns say x and y in an R data frame called df then the scatterplot with displaying 0 at Y-axis can be created by using the below commandggplot(df,aes(x,y))+geom_point()+scale_y_continuous(limits=c(0,”upperlimit”))Consider the below data frame −Example Live Demox

Create Boxplot Using ggplot2 Without Whiskers in R

Nizamuddin Siddiqui
Updated on 06-Feb-2021 08:46:03

884 Views

To create boxplot using ggplot2 without whiskers, we need to use coef argument inside geom_boxplot function. For example, if we have data frame called df and there exists one categorical variable x and one response variable y then the boxplots for categories without whiskers can be created by using ggplot(df,aes(x,y))+geom_boxplot(coef=0).Consider the below data frame −Example Live Demox

Advertisements