Programming Articles - Page 1729 of 3363

How to avoid the warning “Cannot compute exact p-value with ties” while perform correlation test for Spearman’s correlation in R?

Nizamuddin Siddiqui
Updated on 08-Sep-2020 14:45:45

23K+ Views

When the variables are not continuous but could be ranked then we do not use pearson correlation coefficient to find the linear relationship, in this case spearman correlation coefficient comes into the scene. Since the spearman correlation coefficient considers the rank of values, the correlation test ignores the same ranks to find the p-values as a result we get the warning “Cannot compute exact p-value with ties”. This can be avoided by using exact = FALSE inside the cor.test function.ExampleConsider the below vectors and perform spearman correlation test to check the relationship between them − Live Demox1

How to get row index based on a value of an R data frame column?

Nizamuddin Siddiqui
Updated on 08-Sep-2020 14:43:24

5K+ Views

A row of an R data frame can have multiple ways in columns and these values can be numerical, logical, string etc. It is easy to find the values based on row numbers but finding the row numbers based on a value is different. If we want to find the row number for a particular value in a specific column then we can extract the whole row which seems to be a better way and it can be done by using single square brackets to take the subset of the row.ExampleConsider the below data frame − Live Demox1

How to display mean inside boxplot created by using boxplot function in R?

Nizamuddin Siddiqui
Updated on 08-Sep-2020 14:30:54

769 Views

A boxplot shows the median as a measure of center along with other values but we might want to compare the means as well. Therefore, showing mean with a point is likely to be preferred if we want to compare many boxplots. This can be done by using points(mean(“Vector_name”)), if we are plotting the columns of an R data frame then we will reference them instead of vector name.ExampleConsider the below data and the boxplot − Live Demox

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

992 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

329 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

989 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

504 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

761 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

Advertisements