In Data Analysis, we often deal with the comparison of values and this comparison could be also done after finding the closest value to a certain value that might be threshold. For this purpose, we can use filter function of dplyr package along with abs and min function, the abs and min function are required to create the formula for finding the closest value.Consider the below data frame −Example Live DemoCategory
The shapiro test is used to test for the normality of variables and the null hypothesis for this test is the variable is normally distributed. If we have numerical columns in an R data frame then we might to check the normality of all the variables. This can be done with the help of apply function and shapiro.test as shown in the below example.Example Live DemoConsider the below data frame −set.seed(321) x1
Sometimes we want to extract table values, especially in cases when we have a big table. This helps us to understand the frequency for a particular item in the table. To access the table values, we can use single square brackets. For example, if we have a table called TABLE then the first element of the table can accessed by using TABLE[1].Example1 Live Demox1
The important part of a boxplot is Y−axis because it helps to understand the variability in the data and hence, we can remove X−axis labels if we know the data description. To create a boxplot using ggplot2 for single variable without X−axis labels, we can use theme function and set the X−axis labels to blank as shown in the below example.Example Live DemoConsider the below data frame −y
The random sample can be created by using sample function, this random sample gives equal chance for each unit to be selected in the sample, hence it is called simple random sample. If we want to have a sample where each unit has different chance of being selected in the sample then we need to use the argument prob as shown in the below examples.Example1 Live Demox1
To remove dot and number at the end of the string, we can use gsub function. It will search for the pattern of dot and number at the end of the string in the vector then removal of the pattern can be done by using double quotes without space. After that the vector will be passed as shown in the below examples.Example1 Live Demox1
To create a correlation matrix plot, we can use ggpairs function of GGally package. For example, if we have a data frame called df that contains five columns then the correlation matrix plot can be created as ggpairs(df). A correlation matrix plot using ggpairs display correlation value as well as scatterplot and the distribution of variable on diagonal.Example Live DemoConsider the below data frame −set.seed(212) x
To convert list elements into a single string, we can use paste function but firstly we would need to unlist the elements. Also, since we want to create a single string double quotes will be required at the end of the output. For example, if we have a list that contains 5 elements say 1, 2, 3, 4, 5 then conversion of these elements into a single string would be "12345".Example1 Live DemoList1
The above error means plot is not being created yet hence abline function cannot be used to draw anything on the plot. Therefore, a plot needs to be created first to use abline function for creating a line or any other thing. Mostly, abline is used to create regression line on the plot, thus we need to create a scatterplot first before using abline.Example Live DemoConsider the below data frame −x
To replace a complete column in an R data frame, we can set the original one to new values by using delta operator. For example, if we have a data frame called df that contains a column x which 500 has values from normal distribution then to replace it with the normal distribution having a mean of 25 can be done as df$x