Found 33676 Articles for Programming

How to extract the model equation from model object in R?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 11:33:15

2K+ Views

To extract the model equation model object, we can use the model object name with dollar sign and call function. For example, if we have a model object name Model then the model equation can be extracted by using Model$call. This will directly present the equation that was used to create the model.Example1 Live Demox1

How to set the chart title at the bottom using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 11:31:50

2K+ Views

Generally, the chart title is written on the upper side of the plot but sometimes we need to put it in the bottom. This is recommended in situations when the chart title explains something about the plot. For example, if we are plotting a normal distribution then we can use “Approximately Normal” as the chart title in the bottom because we know that perfect normal is a very rare event. If we want to set the chart title at the bottom in a chart created by using ggplot2 then we need to use grid.arrange function of gridExtra package.ExampleConsider the below ... Read More

How to change the thickness of the borders of bars in bar plot created by using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 11:29:58

4K+ Views

The border thickness highlights the bars and this could be useful in situations where we have similar frequencies. If we want to change the thickness of the bars then size argument under geom_bar function of ggplot2 package can be used and it can be set according to our need starting from 1.ExampleConsider the below data frame −x

How to create stacked barplot using barplot function in R?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 11:28:12

428 Views

To create a stacked barplot using barplot function we need to use matrix instead of a data frame object because in R barplot function can be used for a vector or for a matrix only. We must be very careful if we want to create a stacked bar plot using barplot function because bar plots are created for count data only. Here, you will see some examples of count as well as continuous data, carefully read the graphs and understand how the graphs are different from each other.Example1 Live DemoM1

How to remove rows that contains all zeros in an R data frame?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 11:23:54

9K+ Views

Often, we get missing data and sometimes missing data is filled with zeros if zero is not the actual range for a variable. In this type of situations, we can remove the rows where all the values are zero. For this purpose, we can use rowSums function and if the sum is greater than zero then keep the row otherwise neglect it.Example1 Live DemoConsider the below data frame −set.seed(251) x1

How to generate the permutation of x values in y positions with fixed row sums in R?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 11:22:27

138 Views

To generate a permutation of x values in y positions, we can use expand.grid function. For example, if we want to generate three columns for the range of values 0 to 5 then it can be done in R by using the below command − Live Demox

How to calculate mahalanobis distance in R?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 11:18:08

677 Views

The Mahalanobis distance is the relative distance between two cases and the centroid, where centroid can be thought of as an overall mean for multivariate data. We can say that the centroid is the multivariate equivalent of mean. If the mahalanobis distance is zero that means both the cases are very same and positive value of mahalanobis distance represents that the distance between the two variables is large. In R, we can use mahalanobis function to find the malanobis distance.Example1 Live DemoConsider the below data frame −set.seed(981) x1

How to find the frequency of a particular string in a column based on another column in an R data frame using dplyr package?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 11:16:40

587 Views

When we have two or more categorical columns in an R data frame with strings as level of the categories or numbers as strings/integers then we can find the frequency of one based on another. This will help us to identify the cross-column frequencies and we can understand the distribution of one categorical based on another column. To do this with dplyr package, we can use filter function.Example Live DemoConsider the below data frame −Group%filter(Standard=="II")%>%count(Group) OutputGroup n 1 1 1 2 2 1 3 3 2 4 4 1Exampledf1%>%filter(Standard=="III")%>%count(Group) OutputGroup n 1 1 1 2 3 2 3 4 6 4 ... Read More

How to create geometric progression series in R?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 11:15:18

2K+ Views

A geometric progression series is a sequence of numbers in which all the numbers after the first can be found by multiplying the previous one by a fixed number. To generate a geometric progression series in R, we can use seq function. For example, to generate a geometric progression series of 2 by having the difference of multiplication value equal to 1 up to 5 can be found as 2^seq(0, 5, by=1) and the output would be 1, 2, 4, 8, 16, 32.Examples2^seq(0, 5, by=1) [1] 1 2 4 8 16 32 2^seq(0, 5, by=2) [1] 1 4 16 2^seq(0, ... Read More

How to create a sample from an R data frame if weights are assigned to the row values?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 11:14:33

1K+ Views

To create a random sample in R, we can use sample function but if the weight of the values is provided then we need to assign the probability of the values based on the weights. For example, if we have a data frame df that contains a column X with some values and another column Weight with the corresponding weights then a random sample of size 10 can be generated as follows −df[sample(seq_len(nrow(df)),10,prob=df$Weight_x),]Example Live DemoConsider the below data frame −set.seed(1256) x

Advertisements