Articles on Trending Technologies

Technical articles with clear explanations and examples

How to change the order of columns in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Aug-2020 786 Views

Ordering columns might be required when we want to manipulate the data. Manipulation can have several reasons such as cross verification, visualisation, etc. We should also be careful when we change anything in the original data because that might affect our processing. To change the order of columns we can use the single square brackets.ExampleConsider the below data frame −> set.seed(1) > Class Grade Score df df   Class Grade Score 1   a     A     68 2   b     B     39 3   c     C      1 4   ...

Read More

How to create bar chart using ggplot2 with chart sub-title in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Aug-2020 219 Views

There are different ways to express any chart. The more information we can provide in a chart, the better it is because a picture says thousand words. Since nobody likes to read a long-reports, we should have better reporting of charts. Therefore, we can add a chart title as well as chart sub-title in ggplot2 to help the readers.ExampleConsider the below data −> set.seed(1) > x table(x) x 2 3 4 5 6 7 8 9 11 1 3 4 2 4 2 2 1 1 > df library(ggplot2)Creating a simple bar chart −> ggplot(df, aes(x))+ + geom_bar()OutputCreating a ...

Read More

How to create a data frame in R with repeated rows by a sequence of number of times or by a fixed number of times?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Aug-2020 1K+ Views

There are times when duplicated rows in a data frame are required, mainly they are used to extend the data size instead of collecting the raw data. This saves our time but surely it will have some biasedness, which is not recommended. Even though it is not recommended but sometimes it becomes necessary, for example, if it is impossible to collect raw data then we can do it. If we do so then we must specify it in our analysis report. In R, we can use rep function with seq_len and nrows to create a data frame with repeated rows.ExampleConsider ...

Read More

How to join points on a scatterplot with smooth lines in R using plot function?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Aug-2020 381 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

How to find the standard error of mean in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Aug-2020 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

How to find the inverse of a matrix in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Aug-2020 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

How to include a factor level in bar blot using ggplot2 in R if that level has a frequency zero.

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Aug-2020 491 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

How to save matrix created in R as tables in a text file with column names same as the matrix?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Aug-2020 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

How to display a list of plots with the help of grid.arrange in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Aug-2020 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

How to arrange a list of scatterplots in R using grid.arrange?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Aug-2020 578 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
Showing 39961–39970 of 61,248 articles
Advertisements