Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 1143 of 2547
How to display zero frequency for bars in base R barplot?
When we create a barplot in base R, the bars are plotted for all the values in the vector but if we have a gap in the values then the bar with zero frequency for that gap is not plotted. For example, if we have a vector called x that contains 100 values consisting of 0, 1, 3 then the barplot will not represent zero frequency for 2. To solve this problem, we can use factor function in the barplot function as shown in the below examples.Example1> x xOutput [1] 0 1 1 1 3 1 3 1 0 1 1 5 1 8 3 2 3 3 3 3 2 4 2 0 5 3 1 0 1 1 4 1 4 2 3 1 3 [38] 1 4 2 2 5 1 2 5 2 3 1 1 0 1 1 2 3 4 3 1 1 2 1 2 5 1 1 3 3 1 3 1 3 0 4 1 1 [75] 2 2 5 2 2 1 2 2 3 1 4 2 1 3 2 0 2 1 0 4 1 5 2 3 2 0 1 1 1 1 2 3 1 3 1 3 2 [112] 3 2 4 4 1 1 0 2 6 1 4 0 5 0 1 2 2 2 3 5 2 2 2 1 4 2 2 1 0 0 1 4 4 1 3 5 1 [149] 1 5 1 2 0 1 2 2 1 1 2 1 0 5 2 3 2 3 1 4 1 2 2 1 1 1 0 0 2 0 1 2 3 4 4 4 2 [186] 2 2 1 1 1 1 4 3 2 2 2 1 0 5 2Example> barplot(table(factor(x,levels=0:10)))OutputExample2> y barplot(table(factor(y,levels=1:5)))Output
Read MoreFind N % (Remainder with 4) for a large value of N in C++
In this problem, we are given a string num representing a large integer. Our task is to Find N % (Remainder with 4) for a large value of N.Problem Description − we will be finding the remainder of the number with 4.Let’s take an example to understand the problem, Inputnum = 453425245Output1Solution ApproachA simple solution to the problem is by using the fact that the remainder of the number with 4 can be found using the last two digits of the number. So, for any large number, we can find the remainder by dividing the number’s last two digits by ...
Read MoreCasting in Rust Programming
Casting or explicit conversion is only allowed in Rust, there’s no implicit conversion that the compiler of Rust does for us. It is known that, in many cases, implicit conversion can lead to data losses, which is not a good thing.Rules for converting between different types is pretty similar to C. In Rust though, we make use of as keyword when we want to convert from one type to another.Example:Consider the following example:// Suppress all warnings from casts which overflow. #![allow(overflowing_literals)] fn main() { let decimal = 65.43_f32; // Error! No implicit conversion // let ...
Read MoreFind n positive integers that satisfy the given equations in C++
In this problem, we are given three values A, B and N. Our task is to Find n positive integers that satisfy the given equations.Problem Description − We need to find the N positive values that satisfy both the equations, x12 + x22 + … xn2 ≥ A x1 + x2 + … xn ≤ BPrint n values if present, otherwise print -1.Let’s take an example to understand the problem, InputN = 4, A = 65, B = 16Output1 1 1 8ExplanationThe equations are −12 + 12 + 12 + 82 = 1 + 1 + 1 + 64 = ...
Read MoreHow to add proportion total at margins on a table in R?
The proportion total in a table helps us to understand the contribution of each row and each column in the total. Therefore, if we want to find the proportion total at margins, we can use addmargins function if we have the proportion table and if we do not have that table then firstly it needs to be created and then use the addmargins function. For example, if we have a proportion table called prop then the command will be addmargins(prop).Example1Consider the below table of proportions −> x1 x2 x3 x4 x5 x6 x7 x8 table1 table1Output [, ...
Read MoreFind n-th element in a series with only 2 digits (and 7) allowed in C++
In this problem, we are given an integer N, denoting a series of numbers consisting of 4 and 7 only.The series is 4, 7, 44, 47, 74, 77, …The task is to find the n-th element in a series with only 2 digits (and 7) allowed.Let’s take an example to understand the problem, InputN = 4, Output47ExplanationThe series is: 4, 7, 44, 47, ….Solution ApproachA simple solution to the problem is creating the series till Nth number. It’s simple, if the current number’s last digit is 7. Then the last digit of previous and next numbers is 4.So, we will ...
Read MoreHow to find the sum of variables by row in an R data frame?
To find the sum of variables by row we mean the sum of row values in the data frame. This can be easily done with the help of rowSums function. For example, if we have a data frame called df then the sum of variables by row can be found by using the command −rowSums(df)Example1Consider the below data frame −> x1 x2 x3 df1 df1Output x1 x2 x3 1 0 2 3 2 1 0 1 3 1 0 2 4 3 3 2 5 4 2 2 6 3 1 5 7 ...
Read MoreHow to convert a column values to column names in R?
To convert a column values to column names, we can use dcast function of reshape2 package. For example, if we have a data frame called df that contains two columns say x and y, where x is categorical and y is numerical. Now if we want to convert the categories in x as column names then it can be done as dcast(df, y~x).Example1Consider the below data frame −> x1 x2 df1 df1Output x1 x2 1 B 4 2 A 2 3 A 5 4 C 3 5 A 7 6 A 4 7 ...
Read MoreFind n-th node in Postorder traversal of a Binary Tree in C++
In this problem, we are given a binary tree and an integer N. The task is to find the n-th node in Postorder traversal of a Binary Tree.A binary tree has a special condition that each node can have a maximum of two children.Traversal is a process to visit all the nodes of a tree and may print their values too.Let’s take an example to understand the problem, InputN = 6Output3ExplanationPost order traversal of tree − 4, 5, 2, 6, 7, 3, 1Solution ApproachThe idea is to use the post order traversal of the binary tree which is done by ...
Read MoreFrom and Into Traits In Rust Programming
From and Into are two traits that Rust provides us. They are internally linked.From TraitWe make use of From trait when we want to define a trait to how to create itself from any other type. It provides a very simple mechanism with which we can convert between several types.For example, we can easily convert str into a String.ExampleConsider the example shown below:fn main() { let my_str = "hello"; let my_string = String::from(my_str); println!("{}", my_string); }OutputhelloWe can even convert our own types.ExampleConsider the example shown below:use std::convert::From; #[derive(Debug)] struct Num { value: i64, } impl From ...
Read More