Suppose we have a matrix M where M[r][c] represents the height of that cell. If we are currently at top left corner and want to go to the bottom right corner. We can move to adjacent cells (up, down, left, right) only if that the height of that adjacent cell is less than or equal to the current cell's height. We can increase the height of any number of cells before we move, so we have to find the minimum total height that needs to be increased so that we can go to the bottom right cell.So, if the input ... Read More
We can use permn function from combinat package to find the permutations but if we have repeated elements in the vector then the result will not have unique permutations, therefore, we need to use unique function along with the permn function. For example, if we have a vector 1, 2, 1 then the permutations will be (1 2 1), (1 1 2), (1 1 2), (1 2 1), (2 1 1), (2 1 1) and the unique permutations will be (1 2 1), (1 1 2), (2 1 1).Example Live Demox1
A legend helps us to differentiate between the type of values or any another division of values in a data set. These legends can be represented in many ways and two of these ways are straight lines and stars. To represent the legend in a plot created by using plot function with colored straight lines or stars, we need to correct lty and pch arguments.ExampleConsider the below vectors −set.seed(199) x
Frequency polygons are the graphs of the values to understand the shape of the distribution of the values. They are useful in comparing different data sets and visualising cumulative frequency distribution of the data sets. In base R, we can use polygon function to create the frequency polygon but first we should create a line plot for the two variables under consideration.ExampleConsider the below vectors x and y −set.seed(999) x
Suppose we have a binary tree; we have to check whether its height is balanced or not. We know that for a height balanced tree, for every node in the tree, the absolute difference of the height of its left subtree and the height of its right subtree is 0 or 1.So, if the input is likethen the output will be TrueTo solve this, we will follow these steps −Define a function dfs(), this will take node, if node is null, then −return 0l := 1 + dfs(left of node)r := 1 + dfs(right of node)if |l - r| > ... Read More
Often, we find that the values that represent factor levels are recorded as numerical values, therefore, we need to convert those numerical values to factor. In this way, we can use the factor column properly in our analysis otherwise R program will treat the factors as numerical values and the analysis output will be incorrect.Example Live Demodata(mtcars) str(mtcars)Output'data.frame': 32 obs. of 11 variables: $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ... $ cyl : num 6 6 4 6 8 6 8 4 4 6 ... $ disp: num 160 160 108 258 360 ... $ hp : num 110 110 93 110 175 105 245 62 95 123 ... $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ... $ wt : num 2.62 2.88 2.32 3.21 3.44 ... $ qsec: num 16.5 17 18.6 19.4 17 ... $ vs : num 0 0 1 1 0 1 0 1 1 1 ... $ am : num 1 1 1 0 0 0 0 0 0 0 ... $ gear: num 4 4 4 3 3 3 3 4 4 4 ... $ carb: num 4 4 1 1 2 1 4 2 2 4 ... mtcars$cyl
Applying for loop to a vector or a list is no different, we can simply use in the usual manner. For example, if we have a list called List and we want to print all the elements of the list then we can use the code for(i in List){print(i)}, here i refers to the vectors in the List.Example Live DemoList
By default, the area covered by legends for a plot created by using plot function is of full size that is 1 (the area size has a range of 0 to 1, where 1 refers to the full size and 0 refers to none). To reduce the size, we can use cex argument with the legend function as shown in the below example.ExampleConsider the below vectors and the plot created between these two vectors −x
When we find statistical summary of an R data frame, we only get the minimum value, first quartile, median, mean, third quartile, and maximum value but in descriptive there are many other useful measures such as variance, standard deviation, skewness, kurtosis, etc. Therefore, we can use basicStats function of fBasics package for this purpose.Loading fBasics package −library(fBasics)Consider mtcars data in base R −Example Live Demodata(mtcars) head(mtcars, 20)Output mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160.0 110 3.90 ... Read More
Suppose we have a list of points and a number k. The points are in the form (x, y) representing Cartesian coordinates. We can group any two point p1 and p2 if the Euclidean distance between them is