Sometimes missing values are coded and when we perform analysis without replacing those missing values the result of the analysis becomes a little difficult to interpret, especially it is difficult to understand by first time readers.Therefore, we might want to remove rows that contains coded missing values. For this purpose, we can replace the coded missing values with NA and then replace the rows with NA as shown in the below given examples.Example 1Following snippet creates a data frame, if missing values are coded as 1 −x1
To replace zero with previous value in an R data frame column, we can use na.locf function of zoo package but to apply this we first need to replace the zero values with NA.For example, if we have a data frame called df that contains a column say Rate then we can use the below commands to replace 0 values in Rate with previous value by using the below given commands − df$Rate[df$Rate==0]
To create scatterplot for two dependent variables and one independent variable, we can use geom_point function and geom_smooth function of ggplot2 package. Both of these functions will be used twice, where we can define the aesthetics of the plot for each dependent variable as shown in the Example below.ExampleFollowing snippet creates a sample data frame −x
To draw concentric circles, we can use draw.circle function of plotrix package where we can put lwd argument but firstly we would need to create a blank graph with plot function as shown below.For Example, we can create three concentric circles at position X=5 and Y=5 having radius of 1, 2, and 3 by using the below command −draw.circle(5, 5, c(1.5, 1, 0.5), col=c("yellow", "green", "orange"), lwd=2)ExampleConsider the following snippet −plot(1:10, type="n")OutputIf you execute the above given snippet, it generates the following Output −Add the following code to the above snippet −plot(1:10, type="n") library(plotrix) draw.circle(5, 5, c(1.5, 1, 0.5), col=c("yellow", ... Read More
To create matrix diagram, we can use corrplot function of corrplot function. For this purpose, we would need to set is.corr argument to FALSE so that the matrix values will be plotted in the diagram. Otherwise, the corrplot function requires correlation matrix instead of a matrix.Check out the Example given below to understand how it works.ExampleFollowing snippet creates a sample matrix −M
To create line chart for mean covered with minimum and maximum in R, we first need to create columns for row means, row minimum, and row maximum after that geom_line function can be used along with geom_ribbon function of ggplot2 package as shown in the below example.ExampleFollowing snippet creates a sample dataframe.x
If we want to add two columns of an R data frame and each of them contains missing values then the addition of columns can be done in one of the following ways −Adding both the column values if they are numeric.Returning numeric if one of the columns has missing value.Returning NA if both the columns have missing value.To this we can take help of apply function and ifelse function as shown in the below given examples.Example 1Following snippet creates a sample data frame −x1
We can take the transpose of vectors stored in an R list by using Map function with do.call. For example, if we have a list called LIST that contains say ten vectors then we can transpose these vectors by using the command given below − do.call(Map,c(f=c,LIST))Check out the below examples to understand how it works.Example 1To take transpose of vectors stored in an R list, use the command given below −List1
To subset a matrix based on values in a particular column, we can use single square brackets and provide the row and column values. The column values will be set for the columns that we want to subset and the row value will be set for the values of the column using which we want to subset the matrix.Check out the below example to understand how it works.ExampleFollowing snippet creates a matrix −M
To highlight outliers in a boxplot, we can create the boxplot with the help of Boxplot function of car package by defining the id.method.For example, if we have a vector called V then the boxplot of V with highlighted outliers can be created by using the below given command −Boxplot(~V,id.method="y")Example 1To highlight outliers in a boxplot, use the command given below −library(car) x
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP