Articles on Trending Technologies

Technical articles with clear explanations and examples

How to subset unique values from a list in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Sep-2020 521 Views

We know that a list in R can have multiple elements of different data types but they can be the same as well. Whether we have the same type of elements or different ones, we might want to subset the list with unique values, especially in situations where we believe that the values must be same. To do this, we can use unique function.ExampleConsider the below list − Live Demox1

Read More

How to use shapiro wilk test to check normality of an R data frame column?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Sep-2020 779 Views

To apply shapiro wilk test for normality on vectors, we just simply name the vector inside shapiro.test function but if we want to do the same for an R data frame column then the column will have to specify the column in a proper way. For example, if the data frame name is df and the column name is x then the function will work as shapiro.test(df$x).Example Live Demox1

Read More

How to find power of a matrix in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Sep-2020 882 Views

The power of a matrix in R cannot be found directly because there is not function in base R for that. Therefore, for this purpose we can use %^% of expm package. Firstly, we will install the expm package then load it and use %^%. For example, suppose we have a matrix called M and we want to find the M raise to the power 2 then it can be done as − M %^%2ExampleInstalling and Loading expm package −install.packages("expm") library(expm)Example Live DemoM1

Read More

How to increase the size of points on a scatterplot if the points are drawn based on another sequence using ggplot2 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Sep-2020 641 Views

When we draw a scatterplot using ggplot2 with points based on a sequence of values then the size of the points might be very small for the small values. As a result, it becomes a little difficult to view the points. Therefore, we might want to increase the size of those points. It can be done by using scale_size_continuous function in which we can set a range for the points size.ExampleConsider the below data frame − Live Demox

Read More

How to extract elements of a list that do not contain NULL in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Sep-2020 1K+ Views

A list sometimes contains NULL elements along with other elements. Therefore, we might want to get rid of that NULL element so that we can use our list without any hustle. To do this, we can use lapply function with the following syntax −Syntax“List_name”[!unlist(lapply(“List_name”,is.null))]ExampleConsider the below list − Live Demox1

Read More

How to subset one or more sub-elements of a list in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Sep-2020 722 Views

A list contains different type of elements and each of the them can have varying elements. To subset these sub-elements we can use sapply function and use c to subset the number of corresponding sub-elements. For example, if we have a list that contains five elements and each of those elements have ten sub-elements then we can extract 1, 2, 3 etc elements from sub-elements.ExampleConsider the below list − Live Demox1

Read More

How to convert the correlation matrix into a data frame with combination of variables and their correlations in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Sep-2020 2K+ Views

The cor function in R helps us to find the correlation matrix from a data frame or a matrix but the output of it always a matrix as intended. We might want to convert that matrix into a data frame which consists of all combination of variables with their correlation value. It can be done by reading the correlation matrix with as.table and converting that table into a data frame with as.data.frame.ExampleConsider the below data frame − Live Demox1

Read More

How to find the difference between row values starting from bottom of an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Sep-2020 242 Views

If an R data frame contains all numerical columns and we want to find the difference between row values then we will lose first row of the data frame because that will not be subtracted from any row. This can be done by using head function and minus sign. It will work as subtracting the second last row from the last row, then subtracting third last row from the second last row and so on.ExampleConsider the below data frame − Live Demox1

Read More

How to reverse the X-axis labels of scatterplot created by using ggplot2 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Sep-2020 1K+ Views

ExampleThere exists a possibility that one of the variables is recorded in an opposite manner and we want to create a scatterplot using that variable. Therefore, we would need to reverse that variable while plotting. Suppose that variable is an independent variable, hence it will be plotted on X-axis. Thus, to reverse the X-axis labels we can use scale_x_reverse function of ggplot2 package.Consider the below data frame −Example Live Demox

Read More

How to calculate weighted mean in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Sep-2020 8K+ Views

Weighted mean is the average which is determined by finding the sum of the products of weights and the values then dividing this sum by the sum of total weights. If the weights are in proportion then the total sum of the weights should be 1. In base R, we have a function weighted.mean to find the weighted mean in which we just need to pass the vector of values and the vector of weights.Examples Live Demox1

Read More
Showing 38741–38750 of 61,248 articles
Advertisements