Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Nizamuddin Siddiqui
Page 109 of 196
How to select the first and last row based on group column in an R data frame?
Extraction of data is necessary in data analysis because extraction helps us to keep the important information about a data set. This important information could be the first row and the last row of groups as well, also we might want to use these rows for other type of analysis such as comparing the initial and last data values among groups. We can extract or select the first and last row based on group column by using slice function of dplyr package.ExampleConsider the below data frame: > x1 x2 df1 head(df1, 12)Output x1 x2 1 1 3 2 1 4 ...
Read MoreHow to change the background color of a plot created by using plot function in R?
To change the focus of a plot we can do multiple things and one such thing is changing the background of the plot. If the background color of a plot is different than white then obviously it will get attention of the readers because this is unusual as most of the times the plots have white backgrounds, hence if we want to attract readers on the plot then we might use this technique. It can be done by using par(bg= "color_name").ExampleCreating a simple histogram −> x hist(x)OutputExampleCreating histogram with different background colors −> par(bg="green") > hist(x)Output> par(bg="yellow") > hist(x)Outputpar(bg="blue") > ...
Read MoreHow to add a new column to represent the percentage for groups in an R data frame?
In data analysis, we often need to find the percentage of values that exists in a data group. This helps us to understand which value occurs frequently and which one has low frequency. Also, plotting of percentages through pie charts can be done and that gives a better view of the data to the readers. Adding a new column as percentage for groups is not a challenge if we can use mutate function of dplyr package, here you will get the examples from that.Example1> Gender Salary df2 df2Output Gender Salary 1 Male 41734 2 Male 39035 3 Male ...
Read MoreHow to find the mean of corresponding elements of multiple matrices in R?
If the elements of multiple matrices represent the same type of characteristic then we might want to find the mean of those elements. For example, if we have matrices M1, M2, M3, and M4 stored in a list and the first element represent the rate of a particular thing, say Rate of decay of rusty iron during rainy season, then we might want to find the mean of first element of matrix M1, M2, M3, and M4. This mean can be found by using Reduce function.ExampleConsider the below matrices and their list −> M1 M1Output [, 1] [, 2] [, ...
Read MoreHow to add a new column in an R data frame by combining two columns with a special character?
A data frame can have multiple types of column and some of them could be combined to make a single column based on their characteristics. For example, if a column has characters and the other has numbers then we might want to join them by separating with a special character to showcase them as an identity.ExampleConsider the below data frame −> ID Frequency set.seed(111) > ID Frequency df dfOutput ID Frequency 1 A 78 2 B 84 3 C 83 4 D 47 5 E 25 6 F 59 7 G 69 8 ...
Read MoreHow to select columns of an R data frame that are not in a vector?
An R data frame can have so many columns and we might want to select them except a few. In this situation, it is better to extract columns by deselecting the columns that are not needed instead of selecting the columns that we need because the number of columns needed are more than the columns that are not needed. This can be done easily with the help of ! sign and single square brackets.ExampleConsider the below data frame −> Age Gender Salary ID Education Experience df dfOutput ID Gender Age Salary Experience Education 1 ...
Read MoreHow to find the standardized coefficients of a linear regression model in R?
The standardized coefficients in regression are also called beta coefficients and they are obtained by standardizing the dependent and independent variables. Standardization of the dependent and independent variables means that converting the values of these variables in a way that the mean and the standard deviation becomes 0 and 1 respectively. We can find the standardized coefficients of a linear regression model by using scale function while creating the model.ExampleConsider the below data frame −> set.seed(99) > x y df1 df1Output x y 1 1.7139625 1.2542310 2 1.9796581 2.9215504 3 1.5878287 2.7500544 4 1.9438585 ...
Read MoreHow to represent X-axis label of a bar plot with greater than equal to or less than equal to sign using ggplot2 in R?
The values of the categorical variable can be represented by numbers, by characters, by a combination of numbers and characters, by special characters, by numerical signs or any other method. But when we create the bar plot, if the size of a label name is large then we might want to reduce it by representing it with a different word or character or sign that gives the same meaning and it can be done by using expression argument inside scale_x_discrete.ExampleConsider the below data frame −> x y df dfOutput x y 1 0 25 2 100 28 3 150 ...
Read MoreHow to display the legend of a bar plot in a colored box in R?
When we create a bar plot or any other plot with legend, the background of the legend is white but it can be changed to any color with the help of scales package. We can make changes in the legend of a plot using alpha in legend.background argument of theme function. This will help us to change the background color of the legend.Example> x y df dfOutput x y 1 0 25 2 100 28 3 150 32 4 200 25Creating a bar plot with legend −> library(ggplot2) > ggplot(df, aes(x, y, fill=x))+geom_bar(stat="identity")OutputChanging the background color of the legend ...
Read MoreHow to generate a sequence of a date in each month for a fixed number of months using R?
Every month have common dates except few such as February do not have 30 or 31 and even 29 in some years and there are months that contain 30 days while some contains 31 days. Therefore, finding a date say the first date, a middle date, or a last date is not an easy task but it can be done with the help of seq function in base R.Examples> seq(as.Date("2020-01-01"), length=12, by="1 month")Output[1] "2020-01-01" "2020-02-01" "2020-03-01" "2020-04-01" "2020-05-01" [6] "2020-06-01" "2020-07-01" "2020-08-01" "2020-09-01" "2020-10-01" [11] "2020-11-01" "2020-12-01"Example> seq(as.Date("2020-01-01"), length=36, by="1 month") Output[1] "2020-01-01" "2020-02-01" "2020-03-01" "2020-04-01" "2020-05-01" [6] "2020-06-01" "2020-07-01" ...
Read More