R Programming Articles

Page 169 of 174

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

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

How to create a plot for the response variable grouped by two columns using ggplot2 in R?

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

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

How to create a subset of rows or columns of a matrix in R?

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

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

How to join two data frames based one factor column with different levels and the name of the columns in R using dplyr?

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

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

How to select top or bottom n elements of a vector in R?

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

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

How to access elements of nested lists in R?

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

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

How to increase the printing limit in R?

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

When we deal with large data then the problem of printing the data or output of the analysis arises. Due to this problem, it becomes difficult to have a look at our complete but it can be avoided. Before importing any large data or performing any calculation that may result in big output, we can change the limit of the printing by using max.print option.Example> set.seed(1) > sample(1:1000, 555555, replace=TRUE)Output[99681] 223 62  961 304  5  262 519 357 415 167 855 523 268 486 [99695] 370 916 703 179 813 833 177 154 72  789 924 918 486 647 [99709] ...

Read More

How to deal with warning “removed n rows containing missing values” while using ggplot2 in R?

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

The warning “removed n rows containing missing values” occurs when we incorrectly specify the range of the values for X-axis or Y-axis. We can this range in ggplot function using scale_x_continuous(limits=c(?, ?)) for x axis and scale_y_continuous(limits=c(?, ?)) for y axis. If the range will be larger than the actual data range then there will be no warning otherwise, we will get the warning for the number of missing values.ExampleConsider the below data frame −> set.seed(2) > x y df library(ggplot2)Creating the plot with Y-axis limits from 0 to 5−> ggplot(df, aes(x, y))+ + geom_point()+ + scale_y_continuous(limits=c(0, 5)) Warning message: ...

Read More

How to create a bar chart using ggplot2 with facets that are in the order of the data in R?

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

Since visualization is an essential part of data analysis, we should make sure that the plots are created in a form that is easily readable for users. For this purpose, the facets in a bar chart helps us to understand the factor variable levels for another factor. To create such type of bar chart, we can use facet_grid function of ggplot2 package.ExampleConsider the below data frame −> set.seed(99) > y class quantity df library(ggplot2)Creating the plot with class on X-axis and y on Y-axis without any facet −> ggplot(df, aes(class, y))+ + geom_bar(stat="identity")OutputCreating the plot with class on X-axis, y ...

Read More

How to stop printing messages while loading a package in R?

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

There are some annoying messages we get while loading a package in R and they are not useful until and unless we are not loading a new package. Since these messages looks like outputs they might be confusing especially when we are analysing string data. Therefore, we must get rid of them.An example of message while loading BSDA package:>> library(BSDA)Loading required package − latticAttaching package − ‘BSDA’The following object is masked from ‘package:datasets’ −OrangeHere we have some messages while loading the package BSDA but we might not be interested in those messages if we are sure that package is installed ...

Read More
Showing 1681–1690 of 1,740 articles
« Prev 1 167 168 169 170 171 174 Next »
Advertisements