The subsetting of a data frame can be done by using column names as well as column number. Also, we can subset by subsequent as well as non-subsequent column numbers. For example, if we have a data frame df that contains column x, y, z then we can make a subset of x and z by using df[, c(1, 3)].ExampleConsider the below data frame:> set.seed(191) > x1 x2 x3 x4 df1 df1Output x1 x2 x3 ... Read More
To create a line that passes through specified points, we first need to create the plot then using plot function then xspline function can be used to join the points with straight lines. The xspline function is specifically designed to draw curves and hence it can be also used to join points in a plot as shown in the below examples.Example1> plot(rpois(10,5)) > xspline(c(4,3,1),c(7,5,2))Output:Example2> plot(rnorm(10)) > xspline(c(4,3,1),c(0.3,-0.5,-1.5))Output:
The two most easy ways to create an empty plot using ggplot2 are using geom_blank function and also adding the theme_bw along with the geom_blank. The geom_blank will create an empty plot with white gridlines and grey background, on the other hand, addition of theme_bw will create the empty plot with grey gridlines and white background.ExampleConsider the below data frame:Live Demo> set.seed(151) > x y df dfOutput x y 1 -0.05153895 0.3139643 2 0.76573738 0.1816184 3 -0.14673959 0.8201743 4 -0.11318581 1.6005576 5 -0.39551140 0.6770630 6 0.78227595 0.7446956 7 -1.39747811 0.7004385 8 -1.01883832 1.2728014 ... Read More
The Kruskal-Wallis test is the non-parametric analogue of one-way analysis of variance. The non-parametric tests are used in situations when the assumptions of parametric tests are not met. If we find significant difference in Kruskal-Wallis then post hoc tests are done to find where the difference exists. For this purpose, we can perform dunn test. The function of dunn test can be accessed through FSA package.Example1Loading FSA package:> library(FSA)Consider the below data frame:Live Demo> x1 y1 df1 df1Output x1 y1 1 E 1.1191117 2 D 1.1276032 3 D 1.5610692 4 E 1.1585054 5 E 1.0239322 6 C ... Read More
The rgl package is specifically designed to create real-time interactive 3D plots and we can create two 3d plots using plot3d function of this package. Also, these plots can be viewed in the R console at a single point of time with the help of open3d() function.ExampleLoading rgl package:Example> library(rgl) > x y z plot3d(x,y,z)OutputExample> open3d() wgl 12 > plot3d(x,y,z,col=rainbow(5))Output
The list elements of two lists cannot be added directly but we can do this addition by unlisting the elements. To do this, we would need to use lapply function. For example, if we have two lists defined as x and y then the sum of the elements in these lists can be calculated as follows:Examplelapply(seq_along(x), function(i) unlist(x[i])+unlist(y[i]))Example1Live Demo> x1 x1Output[[1]] [1] 0 3 0 1 2 0 1 0 1 3 3 0 0 0 1 1 0 1 0 1 1 0 2 0 0 6 1 2 1 1 1 1 2 1 1 0 0 [38] ... Read More
If a list has the same length of elements (not sub-elements) as the length of each vector for which we want to create the data frame then we first need to create the data frame of vectors then we can easily add the list into the data frame. But if we have a list and other vectors then data frame cannot be created as data.frame function will read each value of the list separately.ExampleLive Demo> df1 df1Output x y 1 6 1 2 8 1 3 6 2 4 8 1 5 5 1 6 3 1 7 6 1 ... Read More
A horizontal line in a histogram is not much useful but we might want to create it in some situations such as where we want to display a specific value on the Y-axis that helps us to differentiate in the density of frequencies above or below a certain value. To create a horizontal line in a histogram, we simply need to use abline function as shown in the below exampleExample> x hist(x) > abline(h=100)OutputExample> abline(h=100,col="blue")OutputExample> abline(h=100,col="blue",lwd=5)Output
To display the superscript in a base R plot, we would need to use expression function inside text function. For example, if we want to display X-square inside a blank plot in base R then we can use the below code:plot(1:10,type="n") text(2,2,expression(X^2))Example1> plot(1:10,type="n") > text(2,2,expression(X^2==4))Output:Example2> text(5,5,expression(X^5==Center))Output:Example3> text(9,9,expression(Squared^2))Output:
The column values of an R data frame can be easily extracted by subsetting with single square brackets but if we want to extract the column values that match a pattern then we need to use grepl function inside single square brackets, this will help us to match the pattern of the values in the data frame columns.ExampleConsider the below data frame:Live Demo> set.seed(271) > x1 x2 df1 df1Output x1 x2 1 A242 B71 2 A123 B71 3 A242 B81 4 A242 B87 5 A123 B71 6 A321 B71 7 ... Read More