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
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
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
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
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
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
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
When there is a common factor with different levels, the joining of data frames is possible but the result will present all the levels with dplyr. We can make use of left_join function to join the two data frames but the size of the first data frame must be greater than the second data frame if they are not same.ExampleConsider the below data frames −> Class df1 df1 Class 1 Statistics 2 Maths 3 Chemistry 4 Physics 5 Economics 6 Political Science 7 Geography > Subject Age df2 df2 Subject Age 1 Maths 18 2 Chemistry 21 3 Physics 22 ... Read More
Selection of top or bottom elements can be done with the help of head and tail function in R. It is required when we want to understand the data in a vector or perform some calculation for partial data.ExampleConsider the below vectors, we will use head and tail to select top and bottom elements in these vectors by using positive and negative signs. These will have a different way to select the elements.> x x [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" [20] "t" "u" "v" "w" ... Read More
Sometimes the lists are contained in another list but we want to access the nested list’s elements. Since these elements are part of a list then cannot be directly accessed, first we need to access the broader list and then the list that contains the element to reach the actual element.ExampleConsider the lists x1, x2, x3, x4, and x4 and the Total_List that contains these lists −> x1 x2 x3 x4 x5 Total_Lists Total_Lists [[1]] [[1]][[1]] [1] 1 2 3 4 5 [[1]][[2]] [1] 6 7 8 9 10 [[1]][[3]] [1] 11 12 13 14 15 [[2]] [[2]][[1]] [1] "a" ... Read More