Found 26504 Articles for Server Side Programming

How to add a straight line to a plot in R starting from bottom left and ending at top right?

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

172 Views

The abline function can give us a straight line from intercept 0 with slope 1 in an existing plot. We would need to pass the coefficients inside the function as abline(coef = c(0,1)). Therefore, we can use this function to add a line starting from bottom left and ending at top right. This is also called diagonal line because it joins the end points on one side with the opposite of the other side.Example> plot(1:10,type="n") > abline(coef=c(0,1))Output:

How to find the mean of row values in an R data frame using dplyr?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:51:50

1K+ Views

The mean of row values can be found by using rowwise function of dplyr package along with the mutate function to add the new column of means in the data frame. The rowwise function actually helps R to read the values in the data frame rowwise and then we can use mean function to find the means as shown in the below examples.Example1Consider the below data frame:Live Demo> x1 x2 df1 df1Output x1 x2 1 0 8 2 2 3 3 2 5 4 0 5 5 3 2 6 0 10 7 3 5 8 1 7 9 0 ... Read More

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

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

189 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

912 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

929 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

Advertisements