Found 2038 Articles for R Programming

How to perform Wilcoxon test for all columns in an R data frame?

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

1K+ Views

Performing Wilcoxon test for all columns in an R data frame means that we want to use this test for single samples and the Wilcoxon test for single sample is used to test for the median of the sample, whether the median is equal to something or not. And if we do not provide any value then zero is the reference value. To perform Wilcoxon test for all columns can be done with the help of apply function and wilcox.test as shown in the below example.Consider the below data frame −Example Live Demox1

How to include a zero with tick in base R plot?

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

310 Views

When we create a plot in base R the Y-axis values are generated automatically and mostly zero is now shown except in few cases that can’t be defined in particular but happens when there exists a zero in data. Therefore, if we want to include a zero with tick in base R plot then ylim argument can be used with the plot function.Exampleplot(5,ylim=c(0,5))OutputExampleplot(rnorm(100),ylim=c(-5,5))OutputExampleplot(rpois(10,2),ylim=c(0,10))Output

How to change the order of elements in a list in R?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 08:01:50

2K+ Views

A list in R can contain many types of elements such as vector, data frame, matrices, etc. Sometimes the order of these elements matter, especially in situations when we have large size elements because it is difficult to view large size elements of a list. This ordering can be done with the help of single square bracket and combine operator c as shown in the below examples.Example Live DemoList1

How to change the name of a data frame in R?

Nizamuddin Siddiqui
Updated on 05-Feb-2021 10:57:44

6K+ Views

To change the name of a data frame, we can set the original name to the new name. Now both of the names can be used. Most of the times the purpose behind changing the name of the data frame is that, the original name does not seem to be a valid name based on the characteristics of the data. For example, if we have normally distributed columns in the data frame then we can name it as normal_distribution. This will help everyone to understand the data belongs to normal distribution.Example1 Live Demoset.seed(24) x

How to check if a variable contains number greater than 1 in an R data frame?

Nizamuddin Siddiqui
Updated on 05-Feb-2021 10:55:19

1K+ Views

The variables in an R data frame are referred to as the columns of the data frame. Sometimes we have a threshold value for a particular column and we need to check whether all the values in that column are greater than or less than the threshold. For this purpose, we can make use of ifelse function as shown in the below examples.Example1 Live DemoConsider the below data frame −set.seed(24) x

How to create a random sample of week days in R?

Nizamuddin Siddiqui
Updated on 05-Feb-2021 10:52:32

292 Views

To create a vector of weekdays we can use the command weekdays(Sys.Date()+0:6) and if we want to create a random sample of week days then sample function can be used along with the weekdays command. For example, if we want to create a random sample of 20 days then it can be done as sample(weekdays(Sys.Date()+0:6),20,replace=TRUE).Examples Live DemoExample1

How to replace missing values with row means in an R data frame?

Nizamuddin Siddiqui
Updated on 05-Feb-2021 10:49:08

640 Views

If we have similar characteristics in each column of an R data frame then we can replace the missing values with row means. To replace the missing values with row means we can use the na.aggregate function of zoo package but we would need to use the transposed version of the data frame as na.aggregate works for column means.Example1Consider the below data frame − Live Demox1

How to find the sum by distinct column for factor levels in an R data frame?

Nizamuddin Siddiqui
Updated on 05-Feb-2021 10:44:52

520 Views

If the data frame contains a factor column and some numerical columns then we might want to find the sum of numerical columns for the factor levels. For this purpose, we can use aggregate function. For example, if we have a data frame df that contains a factor column defined by Group and some numerical columns then the sum by distinct column for factor levels can be calculated by using aggregate(.~Group,data=df,sum)Example1 Live DemoConsider the below data frame −Group

How to create a dotchart using ggplot2 without gridlines in R?

Nizamuddin Siddiqui
Updated on 05-Feb-2021 10:39:53

201 Views

To create a dotchart using ggplot2 in R, we can use geom_dotplot function but the default gridlines will be in the output. If we want to remove the gridlines from the plot then theme function can be added in the rest of the command as theme(panel.grid=element_blank()).Example Live DemoConsider the below data frame −set.seed(214) x

How to change the repeated row names and column names to a sequence in a matrix in R?

Nizamuddin Siddiqui
Updated on 05-Feb-2021 10:37:08

337 Views

To change the repeated row names and column names to a sequence, we first need to read those names in a vector then set them to row names and column names with make.unique function. For example, if a matrix has row names defined as A, B, A, B, A then it can be converted into A, B, A.1, B.1, A.2.Example1 Live DemoM1

Advertisements