Found 33676 Articles for Programming

How to find the mean of all values in an R data frame?

Nizamuddin Siddiqui
Updated on 08-Sep-2020 14:26:29

2K+ Views

If a data frame has all numerical columns then we might be interested in finding the mean of all values in that data frame but this cannot be done directly because a data frame object is not numeric. Therefore, to find the mean of all values in an R data frame, we need to convert it to a matrix first then use the mean function.ExampleConsider the below data frame − Live Demox1

How to find the quantiles in R without quantile name?

Nizamuddin Siddiqui
Updated on 08-Sep-2020 14:15:28

958 Views

The calculation of quantiles in R is very simple, we just need to use quantile function and it returns all the quantiles that are 0%, 25%, 50%, 75% and 100%. If we want to avoid the printing the name of these quantiles then we can use names=FALSE with the quantile function. For example, if we have a vector called x then the quantiles without names can be found as quantile(x,names=FALSE).Example Live Demox1

How to create a matrix with random values in R?

Nizamuddin Siddiqui
Updated on 08-Sep-2020 14:11:16

8K+ Views

Generally, a matrix is created with given values but if we want to create the matrix with random values then we will use the usual method with the matrix function. Random selection in R can be done in many ways depending on our objective, for example, if we want to randomly select values from normal distribution then rnorm function will be used and to store it in a matrix, we will pass it inside matrix function.Example Live DemoM1

How to combine matrices having same number of columns in R?

Nizamuddin Siddiqui
Updated on 08-Sep-2020 13:56:09

300 Views

The matrices that have same number of columns can be combined by rows. For example, if we have five matrices list, each having six columns then those matrices can be converted into a single matric by joining the rows of those matrices. It can be done by using do.call(rbind,”List_of_matrices_object_name”).ExampleConsider the below matrices and their list − Live DemoM1

How to create random sample based on group columns of a data.table in R?

Nizamuddin Siddiqui
Updated on 08-Sep-2020 13:38:21

941 Views

Random sampling helps us to reduce the biasedness in the analysis. If we have data in groups then we might want to find a random sample based on groups. For example, if we have a data frame with a group variable and each group contains ten values then we might want to create a random sample where we will have two values randomly selected from each group. This can be done by using sample function inside .SDExampleConsider the below data.table −library(data.table) Group

How to subset unique values from a list in R?

Nizamuddin Siddiqui
Updated on 08-Sep-2020 13:29:48

464 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

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

Nizamuddin Siddiqui
Updated on 08-Sep-2020 13:26:26

728 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

How to find power of a matrix in R?

Nizamuddin Siddiqui
Updated on 08-Sep-2020 13:23:12

811 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

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
Updated on 08-Sep-2020 13:04:56

577 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

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

Nizamuddin Siddiqui
Updated on 08-Sep-2020 13:01:20

953 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

Advertisements