R Programming Articles

Page 84 of 174

How to add a new column to represent the percentage for groups in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 4K+ Views

In data analysis, we often need to find the percentage of values that exists in a data group. This helps us to understand which value occurs frequently and which one has low frequency. Also, plotting of percentages through pie charts can be done and that gives a better view of the data to the readers. Adding a new column as percentage for groups is not a challenge if we can use mutate function of dplyr package, here you will get the examples from that.Example1> Gender Salary df2 df2Output Gender  Salary 1  Male   41734 2  Male   39035 3  Male   ...

Read More

How to find the mean of corresponding elements of multiple matrices in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 1K+ Views

If the elements of multiple matrices represent the same type of characteristic then we might want to find the mean of those elements. For example, if we have matrices M1, M2, M3, and M4 stored in a list and the first element represent the rate of a particular thing, say Rate of decay of rusty iron during rainy season, then we might want to find the mean of first element of matrix M1, M2, M3, and M4. This mean can be found by using Reduce function.ExampleConsider the below matrices and their list −> M1 M1Output   [, 1] [, 2] [, ...

Read More

How to find the standardized coefficients of a linear regression model in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 2K+ Views

The standardized coefficients in regression are also called beta coefficients and they are obtained by standardizing the dependent and independent variables. Standardization of the dependent and independent variables means that converting the values of these variables in a way that the mean and the standard deviation becomes 0 and 1 respectively. We can find the standardized coefficients of a linear regression model by using scale function while creating the model.ExampleConsider the below data frame −> set.seed(99) > x y df1 df1Output      x       y 1 1.7139625 1.2542310 2 1.9796581 2.9215504 3 1.5878287 2.7500544 4 1.9438585 ...

Read More

How to generate a sequence of a date in each month for a fixed number of months using R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 985 Views

Every month have common dates except few such as February do not have 30 or 31 and even 29 in some years and there are months that contain 30 days while some contains 31 days. Therefore, finding a date say the first date, a middle date, or a last date is not an easy task but it can be done with the help of seq function in base R.Examples> seq(as.Date("2020-01-01"), length=12, by="1 month")Output[1] "2020-01-01" "2020-02-01" "2020-03-01" "2020-04-01" "2020-05-01" [6] "2020-06-01" "2020-07-01" "2020-08-01" "2020-09-01" "2020-10-01" [11] "2020-11-01" "2020-12-01"Example> seq(as.Date("2020-01-01"), length=36, by="1 month") Output[1] "2020-01-01" "2020-02-01" "2020-03-01" "2020-04-01" "2020-05-01" [6] "2020-06-01" "2020-07-01" ...

Read More

How to create two vertical lines on a plot with shaded area in-between using R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 438 Views

Sometimes we want to place a vertical rectangle on a plot that has different color as compared to the rest of the plot area. This vertical rectangle is created based on the conditional values of x axis and represent the pivot area or unimportant area depending on the characteristics of the data. These values of x variable are placed as vertical lines on the plot and the area between these lines is shaded. It can be done by using geom_rect function.ExampleConsider the below data frame −> x y df dfOutput  x y 1 2 9 2 3 7 3 1 ...

Read More

How to extract p-values for intercept and independent variables of a general linear model in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 517 Views

General linear model does not assume that the variables under consideration are normally distributed, therefore, we can use other probability distributions to create a general linear model. We should actually say that if the data does not follow normal distribution then we can try different distributions using general linear model and check whether the model is appropriate or not. The p-values plays an important role in selecting the best model and we might want to extract them from the model object. This can be done by using coef function.ExampleConsider the below data frame −> set.seed(123) > var1 var2 var3 Response ...

Read More

How to check whether the elements of a vector are arranged in an increasing order or decreasing order?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 1K+ Views

A vector can contain values that are increasing or decreasing in nature or they can be also random which means a higher value may come after a lower one which is followed by a higher value. An example of increasing arrangement of elements of vector is 1, 2, 3 and the opposite of that would be decreasing arrangement. We can check whether a vector is arranged in increasing order or decreasing order by checking whether the difference between all values of the vector is greater than or equal to zero or not and it can be done by using diff ...

Read More

How to determine the percentiles of a vector values in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 349 Views

Percentile helps us to determine the values that lie at a certain percent in a data set. For example, if we have a vector of size 100 with containing any values and suppose that the tenth-percentile of the vector is 25, which means there are ten percent values in the vector that are less than 25, or we can say, there are ninety percent values in the vector that are greater than 25. We can find percentiles of a vector values using quantile function in R.Examples> x1 x1Output[1] 7 1 7 6 6 5 3 1 5 5 4 5 ...

Read More

How to change the angle of annotated text in plot created by using ggplot2 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 340 Views

To annotate the text inside a plot created by ggplot2, we can use annotate function. It is used to give some explanation about the plot or add any useful information that will help readers to understand the plot in a better way. Sometimes, we might want to change the angle of the annotated text, especially in cases where we have some information that is presented vertically in the plot, therefore, we can use angle argument of the annotate function.ExampleConsider the below data frame −> x y df dfOutput      x       y 1 4.086537 5.890591 2 2.271184 ...

Read More

How to find the cumulative sums if a vector contains NA values in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 952 Views

The cumulative sums are the sum of consecutive values and we can take this sum for any numerical vector or a column of an R data frame. But if there exits an NA, then we need to skip it and therefore the size of the cumulative sums will be reduced by the number of NA values. If we have NA values in a vector then we can ignore them while calculating the cumulative sums with cumsum function by using !is.na.Examples> x1 x1Output[1] 1 2 3 4 5 6 7 8 9 10 NA > cumsum(x1[!is.na(x1)]) [1] 1 3 6 10 ...

Read More
Showing 831–840 of 1,740 articles
« Prev 1 82 83 84 85 86 174 Next »
Advertisements