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 1144 of 2547
How to add a vector to each row of a matrix in R?
To add a vector to reach row of a matrix, we can use addition sign (+) and create the repetition of the vector up to the number of rows in the matrix. For example, if we have a matrix called M then a vector say v can be added to each row of M by using the command −M+rep(v, each=nrow(M))Example1Consider the below matrix and the vector −> M1 M1Output [, 1] [, 2] [1, ] 3 2 [2, ] 3 3 [3, ] 4 2 [4, ] 5 1 [5, ...
Read MoreHashMap in Rust Programming
HashMap is an important data structure, as it allows us to store data in key-value pairs. In Rust, HashMap stores values by key.HashMap keys can be Boolean, Integer, strings or any other data type that implements the Eq and Hash traits.HashMaps can grow in size, and when the space becomes too excessive, they can also shrink themselves.We can create a HashMap in multiple ways, we can use either HashMap::with_capacity(uint) or HashMap::new().Following are the methods that HashMaps support:insert()get()remove()iter()ExampleLet’s see an example where we build a HashMap and use all these operations stated above.Consider the example shown below.use std::collections::HashMap; fn call(number: ...
Read MoreHow to add suffix to column names in R?
To add suffix to column names in R, we can use paste function. For example, if we have a data frame called df that contains three columns say x, y, and z and we want to add a suffix to these columns say underscore1 (_1) then it can be done by using the commandcolnames(df) x y z df1 df1Output x y z 1 6 3 2 2 9 7 5 3 5 7 6 4 5 9 6 5 2 5 9 6 4 5 4 7 2 0 7 8 2 5 8 9 4 5 8 10 6 ...
Read MoreHow to find the length of columns for missing values in R?
The length of columns for missing values means the number of missing values in the data frame. This can be easily done with the help of colSums function where we will find the total number of NA values with is.na. For example, if we have a data frame called df that contains some missing values then the length of columns for missing values can be found by using the command colSums(is.na(df)).Example1Consider the below data frame −> x1 x2 x3 x4 df1 df1Output x1 x2 x3 x4 1 NA NA 2 2 2 NA NA NA 2 3 1 NA ...
Read MoreLoop Keyword in Rust Programming
Rust provides a loop keyword that allows us to run an infinite loop. The infinite loop indicated by the help of the loop keyword can be broken by the use of the break keyword. Also, we can exit an iteration and continue the infinite loop with the help of the continue keyword.ExampleConsider the example shown below −fn main() { let mut count = 0u32; println!("Infinite loop begins!!"); // Infinite loop loop { count += 1; if count == 4 { println!("four"); continue; ...
Read MoreHow to match a column in a data frame with a column in another data frame in R?
To match a column in a data frame with a column in another data frame, we can use match function. For example, if we have two data frames called df1 and df2 each having one similar column and the second having an extra column then the matching can be done for similar columns and a new column in the first data frame can be created based on that match and the second column the second data frame. Check out the below examples to understand how it works.Example1> df1 df1Output x1 1 2 2 2 3 1 4 ...
Read MoreMatch in Rust Programming
Rust provides us with a match keyword that can be used for pattern matching. It is similar to the switch statement in C, and the first arm that matches is evaluated.ExampleConsider the example shown below −fn main() { let number = 17; println!("Tell me about {}", number); match number { 1 => println!("One!") 2 | 3 | 5 | 7 | 11 => println!("A prime"), 13..=19 => println!("A teen"), _ => println!("Ain't special"), } }In the above example, we are trying to use a ...
Read MorePanic! Macro in Rust Programming
Handling critical errors in Rust is done with the help of panic! Macro. There are other ways to handle errors in Rust, but panic is unique in the sense that it is used to deal with unrecoverable errors.When we execute the panic! Macro, the whole program unwinds from the stack, and hence, it quits. Because of this manner with which the program quits, we commonly use panic! for unrecoverable errors.SyntaxThe syntax of calling a panic looks like this −panic!("An error was encountered");We usually pass a custom message inside the parentheses.ExampleConsider the code shown below as a reference −fn drink(beverage: &str) ...
Read MoreHow to combine a data frame and a named vector if name matches with a column in R?
If we have a data frame that contains a character column and a named vector which has the same names as in the character column of the data frame then we can combine this data frame and the vector by using match function be appropriately defining the names and the character column. Check out the below example to understand how it can be done.ExampleConsider the below data frame df1 and the vector v1 −df1
Read MorePath Struct in Rust Programming
Path struct in Rust is used to represent the file paths in the underlying filesystem. It should also be noted that a Path in Rust is not represented as a UTF-8 string; instead, it is stored as a vector of bytes (Vec).ExampleConsider the example shown below −use std::path::Path; fn main() { // Create a `Path` from an `&'static str` let path = Path::new("."); // The `display` method returns a `Show`able structure let display = path.display(); // Check if the path exists if path.exists() { println!("{} exists", display); } ...
Read More