Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Server Side Programming Articles - Page 1654 of 2646
347 Views
Legends help us to differentiate the values of the response variable while creating the scatterplot. In this way, we can understand how one level of a factor variable affects the response variable. The legend is preferred to be positioned at left bottom, top right, top left, and bottom right. We can use theme function to position the legends.ExampleConsider the below data frame −> set.seed(99) > x1 x2 F df library(ggplot2)Creating the plot with different legend positions −Consider the below data frame −> ggplot(df, aes(x=x1, y=x2, colour=F)) + geom_point(aes(colour=F)) + + theme(legend.justification = c(1, 0), legend.position = c(1, 0))Output> ggplot(df, aes(x=x1, ... Read More
5K+ Views
In data analysis, we often deal with factor variables and these factor variables have different levels. Sometimes, we want to create subset of the data frame in R for specific factor levels to analyze the data only for that particular level of the factor variable. This can be simply done by using subset function.ExampleConsider the below data frame −> set.seed(99) > Factor Percentage df df Factor Percentage 1 India 48 2 China 33 3 USA 44 4 UK 22 5 Canada 62 6 India 32 7 China 13 8 ... Read More
12K+ Views
To convert a vector into matrix, just need to use matrix function. We can also define the number of rows and columns, if required but if the number of values in the vector are not a multiple of the number of rows or columns then R will throw an error as it is not possible to create a matrix for that vector.Here, we will read vectors by their names to make it easy but you can change their names if you want. There are four vectors of different lengths that are shown in these examples −Examples > Vector1 Vector1 [1] ... Read More
432 Views
A stacked bar plot consists multiple bars in one bar, it shows one category for a categorical variable with its levels. Mostly, the stacked bar chart is created with the count of the levels in each category but if we want to create it with percentage for individual categories of the categorical variables then it can be done as well. We can use prop.table function to create the proportion of levels for each category then create the bar plot.ExampleConsider the below data frame −> set.seed(99) > x1 x2 x3 df df x1 x2 x3 1 48 98 68 2 33 ... Read More
487 Views
Lagged variable is the type of variable that contains the previous value of the variable for which we want to create the lagged variable and the first value is neglected. Therefore, we will always have one missing value in each of the groups, if we are creating a lagged variable that depends on a grouping variable or factor variable.ExampleConsider the below data frame:> set.seed(2) > Factor Rate df df Factor Rate 1 F1 12 2 F1 54 3 F1 18 4 F1 26 5 F1 14 6 F2 25 7 F2 81 8 F2 47 9 F2 15 10 F2 ... Read More
938 Views
When we multiple a matrix with a vector in R, the multiplication is done by column but if we want to do it with rows then we can use transpose function. We can multiply the transpose of the matrix with the vector and then take the transpose of that multiplication this will result in the multiplication by rows.ExampleConsider the below matrix −> M1 M1 [,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 25 > V1 M1*V1 [,1] [,2] [,3] [,4] [,5] [1,] 1 6 11 16 21 [2,] 4 14 24 34 44 [3,] 9 24 39 54 69 [4,] 16 36 56 76 96 [5,] 25 50 75 100 125Row-wise Multiplication −> t(t(M1)*V1) [,1] [,2] [,3] [,4] [,5] [1,] 1 12 33 64 105 [2,] 2 14 36 68 110 [3,] 3 16 39 72 115 [4,] 4 18 42 76 120 [5,] 5 20 45 80 125Let’s have a look at one more example −> M2 M2 [,1] [,2] [,3] [,4] [,5] [1,] 72 5 36 11 76 [2,] 61 38 17 73 25 [3,] 96 9 62 79 64 [4,] 77 53 80 78 50 [5,] 81 15 21 43 23 > V2 V2 [1] 28 20 1 68 86 > t(t(M2)*V2) [,1] [,2] [,3] [,4] [,5] [1,] 2016 100 36 748 6536 [2,] 1708 760 17 4964 2150 [3,] 2688 180 62 5372 5504 [4,] 2156 1060 80 5304 4300 [5,] 2268 300 21 2924 1978
891 Views
Vectors are frequently created in R but most of the times we don’t give names to their elements and if we want to give their names then we can use setNames function. This function will help us to name the vector elements in a single line of code, obviously this will save our time and workspace in R.Examples > V1 V1 A B C D E F G H I J 1 2 3 4 5 6 7 8 9 10 > V2 V2 A B C D E F G H I J 1 2 3 4 ... Read More
5K+ Views
Plotting a function is very easy with curve function but we can do it with ggplot2 as well. Since ggplot2 provides a better-looking plot, it is common to use it for plotting instead of other plotting functions. To plot a function, we should specify the function under stat_function in ggplot.ExampleConsider the below data frame −> x df library(ggplot2)Plotting of functions is as shown below:> ggplot(df, aes(x))+ + stat_function(fun=function(x) log(x))> ggplot(df, aes(x))+ + stat_function(fun=function(x) log(x)/x)Output> ggplot(df, aes(x))+ + stat_function(fun=function(x) log(x)/(x-3))Output> ggplot(df, aes(x))+ + stat_function(fun=function(x) (exp(x)^2)*2)OutputRead More
892 Views
When we create line chart with each of the lines having different color, we might want to change the color of lines if the colors we used at the first time are not making the chart attractive. This can be done by manually setting the color of the lines in the chart with the help of scale_color_manual function.ExampleConsider the below data frame −> set.seed(2) > Group Time Frequency df df Group Time Frequency 1 1 Time1 3 2 2 Time2 6 3 3 Time1 5 4 4 Time2 3 5 5 Time1 9 6 1 Time2 9 7 ... Read More
1K+ Views
We save our data files created in R to use them in the future and these files have an extension .Rdata. To view these files, we can make use of load function that will read the path of the file on your system. Suppose you save the file in Documents folder as I do then you will have to provide the path of the Documents folder and that’s it.ExampleSuppose that you created a data frame df and saved it as df.Rdata file in your system −> set.seed(99) > x1 x2 x3 df df x1 x2 x3 1 2 0.7542310 3.3730539 ... Read More