Server Side Programming Articles - Page 1441 of 2650

How to display central limit theorem using uniform random variable in R?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:48:55

197 Views

The central limit theorem says that as the sample size increases the distribution of the sample means approaches normal distribution. Therefore, irrespective of the actual population distribution if we take samples of larger size and find the mean of these samples then the distribution of these sample means will be approximately normal. We can display this in R, by creating the histogram of such type of means.Example1> x y

How to find the residual of a glm model in R?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:46:44

1K+ Views

In a linear model, a residual is the difference between the observed value and the fitted value and it is not different for a general linear model. The difference between linear model and the general linear model is that we use a probability distribution to create a general linear model. If we want to find the residual for a general linear model then resid function can be used just like it is used with the linear model.Example1Consider the below data frame:Live Demo> x1 y1 df1 df1Output x1 y1 1 4 2 2 3 3 3 5 3 4 4 2 ... Read More

How to add a column in an R data frame with consecutive numbers?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:45:10

7K+ Views

Addition of a column with consecutive might have different objectives such as getting the sequence of numbers, representing serial numbers, representing ids, identification of each row, or a variable. We can use the sequence starting from any number up to the number of rows if we know the number of rows for this purpose.Example1Consider the below data frame:Live Demo> x1 x2 df1 df1Output x1 x2 1 6.137898 5.203712 2 5.283467 5.057344 3 5.873749 4.907388 4 7.628762 5.012650 5 4.134700 4.988379 6 5.340686 4.684900 7 5.126999 4.821752 8 3.722762 4.974044 9 ... Read More

How to find the cumulative sum but restarts it if a value is 1 in R?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:42:36

932 Views

Sometimes we want to find the conditional cumulative sums and these conditions can be resetting the cumulative if a particular value occurs. For example, finding the cumulative sum of a variable frame but restarting the sum if 1 occurs. In R, we can do this with the help of with, ave and cumusum function as shown in the below examples.Example1Consider the below data frame:Live Demo> ID Ratings df1 df1Output ID Ratings 1 1 0 2 2 2 3 3 0 4 4 0 5 5 0 6 6 ... Read More

How to find the group-wise correlation coefficient in R?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:40:16

940 Views

If we have two continuous and one categorical column in an R data frame then we can find the correlation coefficient between continuous values for the categories in the categorical column. For this purpose, we can use by function and pass the cor function with the spearman method as shown in the below examples.Example1Consider the below data frame:Live Demo> x1 y1 z1 df1 df1Output x1 y1 z1 1 A 1.1155324 2 2 C 0.9801564 3 3 B 0.9116162 1 4 A 0.8406772 3 5 C 0.8009355 2 6 A 0.9331637 2 7 B 1.0642089 ... Read More

How to subset rows of an R data frame using grepl function?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:38:07

5K+ Views

The grepl function in R search for matches to argument pattern within each element of a character vector or column of an R data frame. If we want to subset rows of an R data frame using grepl then subsetting with single-square brackets and grepl can be used by accessing the column that contains character values.Example1Consider the below data frame:Live Demo> x1 y1 z1 df1 df1Output x1 y1 z1 1 A 0.8833979 5 2 B 0.5400075 1 3 C 0.6923827 3 4 B 1.5069186 2 5 B 0.8190962 2 6 B 0.8296171 1 7 ... Read More

How to find the column means of a column based on another column values that represent factor in an R data frame?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:35:48

2K+ Views

If we have a column that represent factor then we might want to find the mean of values in other column(s) for the factor levels. This is helpful in comparing the levels of the factor. In R, we can find the mean for such type of data by using aggregate function. Check out the below examples to understand how it can be done.Example1Consider the below data frame:Live Demo> x1 y1 df1 df1Output x1 y1 1 D 5.801197 2 B 3.432060 3 B 6.154168 4 A 5.466655 5 D 5.171689 6 C 5.175170 7 B 5.353469 8 D ... Read More

How to convert a string vector into an integer vector in R?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:34:25

2K+ Views

A string vector contains element inside double-quotes and an integer vector does not have any quotes. Sometimes integer values are stored in double-quotes hence the vector of these values is treated as a string vector in R but we need the integer values to perform mathematical operations. Therefore, we can use as.integer function to convert the string vector into an integer vector.Example1Live Demo> x1 x1Output[1] "3" "2" "1" "2" "1" "1" "1" "1" "1" "1" "3" "3" "3" "1" "2" "1" "1" "2" [19] "2" "3" "3" "3" "3" "2" "3" "3" "3" "2" "1" "2" "3" "3" "2" "1" ... Read More

How to find the mean of a square matrix elements by excluding diagonal elements in R?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:32:40

275 Views

There are many ways to find the mean of a matrix elements by excluding diagonal elements, this mean is actually the mean of lower triangular matrix and the upper triangular matrix. We can simply use mean function by creating a vector of lower and upper triangular matrix as shown in the below examples.Example1Live Demo> M1 M1Output   [, 1] [, 2] [, 3] [, 4] [1, ] 1    6    3    6 [2, ] 8    5    3    4 [3, ] 5    4    4    6 [4, ] 5    5    3    4 ... Read More

How to create a bar plot using ggplot2 with one bar having black border in R?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:28:24

934 Views

The bar plot can be easily created with the help of geom_bar. But if we want to have a different border for a particular bar then we first need to create the bar plot and store it in an object. After that we need to add the original plot with the bar for which we want to have a black border. Check out the below example to understand how it can be done.ExampleConsider the below data frame:Live Demo> Group Freq df dfOutput Group Freq 1 G1 18 2 G2 27 3 G3 24Loading ggplot2 ... Read More

Advertisements