Found 2038 Articles for R Programming

How to make all the elements in a list of equal size in R?

Nizamuddin Siddiqui
Updated on 19-Oct-2020 14:26:35

361 Views

We know that a list can multiple elements of different types as well as of different size. For example, a list that contains two elements then one element may contain fifteen elements and the other might have twenty-five elements. In this situation, we might want to fill the first element with ten more elements so that the size of both the elements become equal. This can be done by using lapply function as shown in the below examples.Consider the below list −Example Live Demoset.seed(101) x1

How to generate Bernoulli random variable in R?

Nizamuddin Siddiqui
Updated on 19-Oct-2020 14:23:16

5K+ Views

Each value in Bernoulli random variable represents success or a failure for a single trial that makes it different from Binomial random variable because a Binomial random variable represents number of success or failure for a number of trials. To generate a Bernoulli random variable, we can use rbinom function but we need to pass 1 for size argument.Example Live Demorbinom(120, 1, 0.71)Output[1] 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 1 1 1 1 0 ... Read More

How to count the number of occurrences of all unique values in an R data frame?

Nizamuddin Siddiqui
Updated on 19-Oct-2020 14:17:05

694 Views

A data frame in R can have infinite number of unique values and it can also contain many repeated values. Therefore, finding the number of all unique values in the data frame can help us to understand the diversity in the data but this most done in situations where we expect to have repeated elements otherwise it would not make sense. To count the number of occurrences of all unique values, we can use table function along with the unlist as shown in the below examples.Consider the below data frame −Example Live Demox1

How to perform homogeneity of variance test for two-way anova in R?

Nizamuddin Siddiqui
Updated on 19-Oct-2020 13:53:02

1K+ Views

In general, we can say that the homogeneity of variance test is the type of test that compares the variance of two or more variables and finds the significant difference between or among them if exists. For a two-way anova, one of the most commonly used homogeneity of variance test is Levene’s Test and it can be easily done with the help of leveneTest function of car package in base R.Consider the below data frame −Example Live Demoset.seed(151) x1F) group 6 0.6593 0.6835 13

How to calculate the number of elements greater than a certain value in a vector in R?

Nizamuddin Siddiqui
Updated on 19-Oct-2020 13:50:16

8K+ Views

In data analysis, sometimes we need to count the number of values that are greater than or less than a certain value, and this certain value could be a threshold. For example, we might have a vector that contain values for blood pressure of people and we might want check how many values are greater than 120. In this type of situation, we can use length function as shown in the below examples.Example Live Demox11])Output[1] 9 Example Live Demox25])Output[1] 93Example Live Demox35])Output[1] 42Example Live Demox40])Output[1] 108Example Live Demox51])Output[1] 107Example Live Demox65])Output[1] 31Example Live Demox71])Output[1] 21Example Live Demox84])Output[1] 19Example Live Demox9118])Output[1] 11Example Live Demox105000])Output[1] 68Read More

How to replicate a vector to create matrix in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 14:44:04

2K+ Views

The matrix can be created by using matrix function in R and if we want to create a matrix by replicating a vector then we just need to focus on the replication. For example, if we have a vector V and we want to create matrix by replicating V two times then the matrix can be created as matrix(replicate(2,V),nrow=2).Example1 Live DemoV1

How to align the bars of a barplot with the X-axis using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 14:16:21

1K+ Views

The bar plot is created with geom_bar function but there always exist some space between the bars and the X-axis labels. If we want to reduce that space or completely remove it we need to use scale_y_continuous function by defining expand argument for former and scale_y_continuous(expand=c(0,0)) for latter.Example Live DemoConsider the below data frame −set.seed(888) x

How to multiply a matrix with a vector in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 14:13:41

5K+ Views

When we multiply a matrix with a vector the output is a vector. Suppose we have a matrix M and vector V then they can be multiplied as M%*%V. To understand the step-by-step multiplication, we can multiply each value in the vector with the row values in matrix and find out the sum of that multiplication.Example1 Live DemoM1

How to replicate whole data frame and add it in the original one in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 14:04:48

1K+ Views

The replicates of a data frame in R can be created with the help of sapply function, to set the number of times we want to repeat the data frame we can use rep.int,times argument. For example, if we have a data frame df and we want to create 5 replicates of df and add them in the original then sapply(df,rep.int,times=5) can be used.Example Live DemoConsider the below data frame −set.seed(151) x1

How to find the day of the year from dates in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 14:02:00

6K+ Views

To find the day of the year from dates, we can use yday function of lubridate package. For example, if we have a date or a date of vectors then we simply need to pass that date or the vector inside yday function by using the below syntax −yday(“date”)oryday(“vector_of_date”)Loading lubridate package −library(lubridate)Examplesdate1

Advertisements