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
Programming Articles - Page 909 of 3363
160 Views
By default, the distance/space between facets created by using ggplot2 is very less and it becomes a little uneasy for viewers to separately read the facets. Therefore, to deal with this problem we can increase the space between facets and it can be done with the help of theme function as shown in the example below.ExampleFollowing snippet creates a sample data frame &miuns;x
530 Views
To find the column mean of all matrices stored in an R list, we can use sapply function along with colMeans function.For example, if we have a list called LIST that contains some matrices then the col means for each matrix can be found by using the commandsapply(LIST,colMeans)Check out the below example to understand how it works.ExampleFollowing snippet creates list of matrices −M1
402 Views
We can find the total number of elements for a matrix with the help of prod and dim function as shown in the below Examples. To create a matrix for odd number of elements by filling the last element with NA, we can use byrow argument.For Example, if we have a vector called V that contains 19 elements then we can create a matrix called M having 20 elements NA as the last element by using the below command −M
5K+ Views
To randomly assign participants to groups, we can use sample function.For example, if we have a data frame called df that contains a column say Employee_ID and we want to create five groups that are stored in a vector say Grp then random assignment of participants to values in Grp can be done by using the command given below −df$Grp
633 Views
To create an integer column in an R data frame with leading zeros, we can use sprintf function.For Example, if we want to create a data frame column having values starting from 1 to 10 and we want to have 1 as 01 and so on then we can use the command given below −data.frame(x=sprintf('%0.2d',1:10))Check out the Examples given below to understand how it works.Example 1To create an integer column in an R data frame with leading zeros, use the command given below −df1
10K+ Views
The easiest way to change the Y-axis title in base R plot is by using the ylab argument where we can simply type in the title. But the use of ylab does not help us to make changes in the axis title hence it is better to use mtext function, using which we can change the font size, position etc. of the title.Check out the below example to understand how it can be done.ExampleUse the code given below to change the Y-axis title in base R plot −plot(1:10)OutputIf you execute the above given code, it generates the following Output −Add ... Read More
3K+ Views
To display x-bar in base R plot, we can use text function and define the bar with bar function inside expression function.For example, if we have a vector called x that contains first ten numbers (1 to 10) then we can display it’s mean inside the base R plot by using the command given below −text(0.97, 5.5, expression(bar("x")))Check out the examples given below to understand how it works.Example 1Use the code given below to display x-bar in base R plot −plot(mean(1:100)) text(0.97, 50.5, expression(bar("x")))OutputIf you execute the above given snippet, it generates the following Output −Example 2Use the code given ... Read More
2K+ Views
To color scatterplot points based on a threshold using ggplot2, we first need to define a column with the threshold value and then we can use that column inside aes for coloring. The column with threshold can be created by using cut function.Check out the example given below to understand how it can be done.ExampleFollowing snippet creates a sample data frame −x
1K+ Views
To find the number of non-missing values in each column by group in an R data frame, we can use summarise_each function of dplyr package with negation of is.na function.For Example, if we have a data frame called df that contains a grouping column say G and some other columns having few NAs then we can find the number of non-missing values in each column by grouping column G with the help of below command −df%%group_by(G)%%summarise_each(funs(sum(!is.na(.))))Example 1Following snippet creates a sample data frame −Group
1K+ Views
To create multiple regression lines using ggplot2, we can use grouping inside aes.For example, if we have a data frame called that contains two numerical columns say x and y and a categorical column say C then the regression lines between x and y for all the categories in C can be created by using the below given command −ggplot(df, aes(x, y, group=C))+geom_point()+stat_smooth(method="lm")ExampleFollowing snippet creates a sample data frame −data(mtcars) head(mtcars, 20)OutputThe following dataframe is created − mpg cyl disp hp drat wt qsec vs am gear carb Mazda ... Read More