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 53 of 196
How to make a plot title partially bold using ggplot2 in R?
To make a plot title partially bold using ggplot2, we can use bquote function inside labs function and then changing the default font to bold using bold function as shown in the below examples. While using these functions we need to make sure that the title that we want to bold should be inside circular brackets appropriately.ExampleConsider the below data frame −> x y df dfOutput x y 1 -0.62160328 0.38477515 2 0.68287365 -1.56169067 3 0.75259774 1.28849990 4 0.56688920 -0.17014225 5 1.22351113 -0.32446764 6 -1.54210099 0.29001967 ...
Read MoreHow to create a plot using ggplot2 by including values greater than a certain value in R?
To create a plot using ggplot2 by excluding values greater than a certain value, we can use subsetting with single square brackets and which function. For example, if we have a data frame called df that contains two columns say x and y, then the point chart by including values of x that are greater than 0 can be created by using the command −ggplot(df[which(df$x>0), ], aes(x, y))+geom_point()ExampleConsider the below data frame −> x y df dfOutput x y 1 -0.62160328 0.38477515 2 0.68287365 -1.56169067 3 0.75259774 1.28849990 ...
Read MoreHow to generate random numbers with sequence and store in a data frame column in R?
To generate random numbers with sequence and storing them in a data frame column, we can use sample function with seq. For example, if we want to create a data frame column having random sequence of values of size 50 between 1 to 100 by considering every tenth value then we can use the commanddf
Read MoreHow to create stacked plot with density using ggplot2 in R?
To create stacked plot with density using ggplot2, we can use geom_density function of ggplot2 package and position="stack". For example, if we have a data frame called df that contains two columns say x and y, where x is categorical and y is numerical then the stacked plot with density can be created by using the command −ggplot(df, aes(y, y=..density..))+geom_density(aes(fill=x), position="stack")ExampleConsider the below data frame −> x y df dfOutput x y 1 C 3 2 C 5 3 B 4 4 A 7 5 B 1 6 A 6 7 D 4 8 C 3 9 C 7 10 ...
Read MoreHow to find the position of odd numbers in an R vector?
To find the position of odd numbers in an R vector, we can find the position of values that are divisible by 2 with the help of which function. For example, if we have a vector called x then we can find the position of odd numbers using the command which(x%%2==1). Check out the below examples to understand how it works.Examplex1
Read MoreHow to select rows of an R data frame that are non-NA?
To select rows of an R data frame that are non-Na, we can use complete.cases function with single square brackets. For example, if we have a data frame called that contains some missing values (NA) then the selection of rows that are non-NA can be done by using the command df[complete.cases(df), ].Example1Consider the below data frame −> x1 x2 x3 df1 df1Output x1 x2 x3 1 1 NA NA 2 NA 5 3 3 1 5 NA 4 1 NA NA 5 NA 5 NA 6 NA 5 3 7 NA 5 NA 8 1 NA ...
Read MoreHow to add name to data frame columns in R?
A data frame can be created by using data.frame function but in this case R generates the column names using the values we pass for the data. For example, if we pass a probability distribution as shown in the below examples then its name will be there. If we want to change those names then setNames function can be used along with the data frame name.Exampledf1
Read MoreHow to display the data frame summary in vertical order in R?
To display the data frame summary in vertical order, we can use lapply and cbind function along with the summary function. For example, if we have a data frame called df then the summary of df in vertical order can be found by using the below command −lapply(df, function(x) cbind(summary(x)))Example1Consider the below data frame −> x1 x2 x3 x4 x5 df1 df1Output x1 x2 x3 x4 x5 1 4 0 2 2 6 2 7 2 4 1 7 3 7 2 3 3 6 4 4 0 4 5 2 5 5 2 2 ...
Read MoreHow to convert an array into a matrix in R?
To convert an array into a matrix in R, we can use apply function. For example, if we have an array called ARRAY that contains 2 array elements then we can convert this array into a single matrix using the command apply(ARRAY,2,c). We need to understand the dimension of the array making the conversion otherwise the output will not be as expected.ExampleConsider the below array −x1
Read MoreHow to find the sum by two factor columns in R?
To find the sum by two factor columns, we can use aggregate function. This is mostly required when we have frequency/count data for two factors. For example, if we have a data frame called df that contains two factor columns say f1 and f2 and one numerical column say Count then the sum of Count by f1 and f2 can be calculated by using the command aggregate(Count~f1+f2,data=df,sum).ExampleConsider the below data frame −x1
Read More