Sometimes values are missing in a sequence and R program records them as NA (Not Available). In this type of situation, we might want to replace consecutive NA records with single NA value. This can be done by using is.na along with diff function as shown in the below examples.Example Live Demox1
To create side by side histograms in base R, we first need to create a histogram using hist function by defining a larger limit of X-axis with xlim argument. After that we can create another histogram that has the larger mean and smaller standard deviation so that the bars do not clash with each other and add=T argument must also be added inside the second hist function.Example Live Demohist(rnorm(5000,mean=5,sd=2.1),col="green",xlim=c(1,20))OutputExamplehist(rnorm(5000,mean=15,sd=1.25),col="red",add=T)Output
We can easily identify duplicate values in a matrix by using duplicated function but it does not specify that the first occurrence is also duplicated. Therefore, we need to use it with OR sign | and the argument fromLast = TRUE of duplicated function so that the first occurrence of the duplicated values will be also identified as duplicate.Example Live DemoM1
Sometimes analysis requires the user to check if values in two columns of an R data frame are exactly the same or not, this is helpful to analyze very large data frames if we suspect the comparative values in two columns. This can be easily done with the help of ifelse function.ExampleConsider the below data frame − Live Demox1
To create a regression line with 0 intercept and slope equals to 1 using ggplot2, we can use geom_abline function but we need to pass the appropriate limits for the x axis and y axis values. For example, if we have two columns x and y in a data frame df and both have ranges starting from -1 to 1 then the scatterplot with regression line with 0 intercept and slope equals to 1 can be created as −ggplot(df,aes(x,y))+geom_point()+geom_abline()+lims(x=c(-1,1),y=c(-1,1))ExampleConsider the below data frame − Live Demox
To convert a time series object into a vector, we just need to read that object with as.numeric and store it in some other object or in the same object. For example if we have a time series object x then it can be converted to a vector by using x
When we receive data from any source, it is highly likely that it would not be a perfect data set for the intended analysis, therefore, we need to perform some cleaning or mining based on the characteristics of the data. For example, if we have a column name of a data frame as factor levels of a numerical variable then we might want to convert that data frame in such a way that numerical values are stored in a single column and the column names are stored in another column that will represent a factor so that we can apply ... Read More
To perform the one-way anova with sample sizes having different sizes we can use aov function. Suppose we have a categorical column defined as Group with four categories and a continuous variable Response both stored in a data frame called df then the one-way anova can be performed as −aov(Response~Group,data=df)ExampleConsider the below data frame − Live DemoGroup
A standard normal distribution is the type of distribution that has mean equals to zero with standard deviation 1. If we want to generate standard normal random numbers then rnorm function of R can be used but need to pass the mean = 0 and standard deviation = 1 inside this function.Example Live Demornorm(10, 0, 1)Output[1] 0.6936607 -0.7967657 -2.7544428 0.2688767 0.5278463 -1.5387568 [7] 1.1716632 -1.5033895 0.8112929 -1.0101065Example Live Demornorm(50, 0, 1)Output[1] 2.58246666 -0.53083341 -0.57343343 1.08172756 1.30341849 -0.07440422 [7] -0.41869305 -0.96227706 -0.46899119 1.55428279 0.09162738 -0.96027221 [13] -0.84735327 -1.74949782 0.58541758 0.23117630 0.47402479 -0.72453853 [19] 0.07171564 1.13088794 0.18735157 0.25091758 -1.34728315 -0.39768159 [25] -0.38109955 -0.34019286 -1.51778561 ... Read More
Data analysis not only includes mathematical operations, we have to spend a lot of time in data cleaning, data mining, feature engineering etc. One operation could be removing list elements by their name if they are not helpful to achieve our objective. The removal of the elements by using their names can be done with the help of which function.ExampleConsider the below list Live DemoList1