Server Side Programming Articles - Page 1535 of 2646

How to use for loop to print all the elements of a list in R?

Nizamuddin Siddiqui
Updated on 08-Oct-2020 14:41:45

2K+ Views

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

How to reduce the size of the area covered by legend in R for a plot created by using plot function?

Nizamuddin Siddiqui
Updated on 08-Oct-2020 14:39:15

1K+ Views

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

Program to check whether a tree is height balanced or not in C++

Arnab Chakraborty
Updated on 08-Oct-2020 14:46:10

649 Views

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

Program to group a set of points into k different groups in Python

Arnab Chakraborty
Updated on 08-Oct-2020 14:28:00

1K+ Views

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

How to find the statistical summary of an R data frame with all the descriptive statistics?

Nizamuddin Siddiqui
Updated on 08-Oct-2020 14:36:44

477 Views

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

Program to count number of strings we can make using grammar rules in Python

Arnab Chakraborty
Updated on 08-Oct-2020 14:22:32

280 Views

Suppose we have a number n, we have to find the number of strings of length n can be generated using the following rules −Each character is a lower case vowel [a, e, i, o, u]"a" may only be followed by one "e""e" may only be followed by any of "a" and "i""i" may not be followed by another "i""o" may only be followed by any of "i" and "u""u" may only be followed by one "a"If the result is very large, mod the result by 10^9 + 7.So, if the input is like n = 2, then the output ... Read More

How to display R-squared value on scatterplot with regression model line in R?

Nizamuddin Siddiqui
Updated on 08-Oct-2020 14:20:27

9K+ Views

The R-squared value is the coefficient of determination, it gives us the percentage or proportion of variation in dependent variable explained by the independent variable. To display this value on the scatterplot with regression model line without taking help from any package, we can use plot function with abline and legend functions.Consider the below data frame −Example Live Demoset.seed(1234) x

How to find the absolute pairwise difference among values of a vector in R?

Nizamuddin Siddiqui
Updated on 08-Oct-2020 14:15:54

519 Views

If a vector contains five values then there will be ten pairwise differences. For example, suppose we have five numbers starting from 1, then the pairwise combinations for these values will be (1,2), (1,3), (1,4), (1,5), (2,3), (2,4), (2,5), (3,4), (3,5), (4,5). Now to find the absolute pairwise differences, we would be need to find the differences between each of these combinations and take the absolute value of the answer hence the result will be 1, 2, 3, 4, 1, 2, 3, 1, 2, 1.Example Live Demox1

Program to find total mutation group of genes in C++

Arnab Chakraborty
Updated on 08-Oct-2020 14:17:07

383 Views

Suppose we have a list of strings called genes where each element has the same length and each element contains characters "A", "C", "G" and/or "T". Now there are some rules −When two strings s1 and s2 are the same string except for one character, then s1 and s2 are in the same mutation group.When two strings s1 and s2 are in a group and s2 and s3 are in a group, then s1 and s3 are in the same group.We have to find the total number of mutation groups we can generate.So, if the input is like genes = ... Read More

How to create a sequence of dates by using starting date in R?

Nizamuddin Siddiqui
Updated on 08-Oct-2020 14:11:47

1K+ Views

The best way to create a sequence of anything is creating it with the help of seq function and this also applies to sequences of dates. But in case of dates, we need to read the dates in date format so that R can understand the input type and create the appropriate vector. If we do not use the date format for the date value then it won’t make sense to R and it will result in error.Examples Live Demox1

Advertisements