Create Data Frame of Maximum Value for Each Group in R using dplyr

Nizamuddin Siddiqui
Updated on 10-Aug-2020 14:06:37

480 Views

Sometimes subsetting of group wise maximum values is required while doing the data analysis and this subset of the data frame is used for comparative analysis. The main objective is to compare these maximums with each other or with a threshold value. In R, we can find the group wise maximum value by using group_by and slice functions in dplyr package.ExampleConsider the below data frame −> x y df head(df, 20) x y 1 S1 1 2 S1 2 3 S1 3 4 S1 4 5 ... Read More

Join Points on a Scatterplot with Smooth Lines in R

Nizamuddin Siddiqui
Updated on 10-Aug-2020 14:04:45

342 Views

It is very difficult to join points on a scatterplot with smooth lines if the scatteredness is high but we might want to look at the smoothness that cannot be understood by just looking at the points. It is also helpful to understand whether the model is linear or not. We can do this by plotting the model with loess using plot function.ExampleConsider the below data −> set.seed(3) > x y Model summary(Model) Call: loess(formula = y ~ x) Number of Observations: 10 Equivalent Number of Parameters: 4.77 Residual Standard Error: 8.608 Trace of smoother matrix: 5.27 (exact) Control ... Read More

Find the Standard Error of Mean in R

Nizamuddin Siddiqui
Updated on 10-Aug-2020 14:03:41

1K+ Views

The standard error of mean is the standard deviation divided by the square root of the sample size. The easiest way to find the standard error of mean is using the formula to find its value.Example> set.seed(1)We will find the standard errors for a normal random variable, sequence of numbers from one to hundred, a random sample, a binomial random variable, and uniform random variable using the same formula. And at the end, I will confirm whether we used the correct method or not for all types of variables we have considered here.> x x [1] -0.6264538 0.1836433 -0.8356286 ... Read More

Find Inverse of a Matrix in R

Nizamuddin Siddiqui
Updated on 10-Aug-2020 14:02:10

8K+ Views

The inverse of a matrix can be calculated in R with the help of solve function, most of the times people who don’t use R frequently mistakenly use inv function for this purpose but there is no function called inv in base R to find the inverse of a matrix.ExampleConsider the below matrices and their inverses −> M1 M1 M1    [, 1] [, 2] [1, ] 1 3 [2, ] 2 4 > solve(M1) [, 1] [, 2] [1, ] -2 1.5 [2, ] 1 -0.5 > M2 M2 ... Read More

Include Factor Level in Bar Plot Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 10-Aug-2020 13:57:01

450 Views

In research, sometimes we get a count of zero for a particular level of a factor variable but we might want to plot that in the bar plot so that anyone who look at the plot can easily understand what is missing and compare all the factor levels. In ggplot2, it can be done with the help of scale_x_discrete function.> x df df$x df$x [1] S1 S2 S3 S4 S1 S2 S3 S4 S1 S2 S3 S4 S1 S2 S3 S4 S1 S2 S3 S4 Levels: S1 S2 S3 S4 S5Loading ggplot2 package −> library(ggplot2)Now when ... Read More

Save Matrix as Tables in a Text File in R

Nizamuddin Siddiqui
Updated on 10-Aug-2020 13:55:43

4K+ Views

Matrix data is sometimes need to be saved as table in text files, the reason behind this is storage capacity of text files. But when we save a matrix as text files in R, the column names are misplaced therefore we need to take care of those names and it can be done by setting column names to the desired value.> M M       [, 1] [, 2] [, 3] [, 4] [1, ] 1 5 9 13 [2, ] 2 ... Read More

Display a List of Plots Using Grid Arrange in R

Nizamuddin Siddiqui
Updated on 10-Aug-2020 13:49:28

2K+ Views

In data analysis, we deal with many variables at a time and we want to visualize the histogram of these variables at a time. This helps us to understand the distribution of each variable in the data set, therefore we can apply the appropriate technique to deal with those variables. To create a list of plots we can use grid.arrange function in gridExtra package that can arrange plots based on our need.ExampleConsider the below data frame −> set.seed(10) > df head(df, 20)             x1     x2           x3     ... Read More

Arrange Scatterplots in R Using Grid Arrange

Nizamuddin Siddiqui
Updated on 10-Aug-2020 13:46:12

539 Views

In predictive modeling, we get so many variables in our data set and we want to visualize the relationship among these variables at a time. This helps us to understand how one variable changes with the other, and on the basis of that we can use the better modeling technique. To create a list of plots we can use grid.arrange function in gridExtra package that can arrange plots based on our need.ExampleConsider the below data frame −> set.seed(10) > df head(df, 20)        x1            x2        x3     x4 1 ... Read More

Create a Plot Grouped by Two Columns Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 10-Aug-2020 13:38:28

137 Views

When two categorical variables make an impact on the response variable together then it is necessary to visualize their effect graphically because this graph helps us to understand the variation in the effect. Therefore, we can create a plot for the response variable that changes with one or both of the categorical independent variables. This can be done with the help of using interaction function in ggplot2.ExampleConsider the below data frame −> set.seed(1) > y Group1 Group2 df head(df, 20) y Group1 Group2 1    1 a Ph1 2    1 b Ph1 3    2 c Ph1 4   ... Read More

Create Subset of Rows or Columns of a Matrix in R

Nizamuddin Siddiqui
Updated on 10-Aug-2020 13:26:10

2K+ Views

A matrix can have multiple rows and columns like a data frame. As in data frames, we sometimes require to take subsets, the same might be required with matrices. But subsetting matrices data is quite simple as compared to subsetting a data frame.ExampleConsider the below matrix −> M M [, 1] [, 2] [, 3] [, 4] [, 5] [1, ] 1 6 11 16 21 [2, ] 2 7 12 17 22 [3, ] 3 8 13 18 23 [4, ] 4 9 14 19 24 [5, ] 5 10 15 20 25Subsetting columns of matrix M −> M[, ... Read More

Advertisements