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
Server Side Programming Articles
Page 1199 of 2109
How to replace a sub-string with the reverse of that sub-string in R?
The chartr function in base R helps us to replace old strings with new strings and hence it can be also used to replace a subs-string with the reverse of that substring. For example, if we have a vector say x that contains tutorialpsoint and we want to convert it to tutorialspoint then it can be done as chartr("tutorialpsoint ", " tutorialspoint ", x).Example1> x1 x1Output[1] "IDNIA"Example> chartr("DN", "ND", x1)Output[1] "INDIA" Example2> x2 x2Output[1] "IDNIA" "IDNIA" "IDNIA" "IDNONESIA" "IDNIA" "IDNONESIA" [7] "IDNONESIA" "IDNIA" "IDNONESIA" "IDNIA" "IDNIA" "IDNONESIA" [13] "IDNONESIA" "IDNONESIA" "IDNIA" "IDNONESIA" "IDNIA" "IDNIA" [19] "IDNONESIA" "IDNONESIA" "IDNIA" "IDNONESIA" "IDNIA" ...
Read MoreHow to find the range of a vector in R?
The range function in R provides the minimum and maximum values instead of the difference between the two. Hence, we can find the minimum and maximum by using range function then diff function can be used to find the actual range. For example, if we have a vector x then the range can be found by using diff(range(x)).Example> x1 x1Output[1] 4 2 3 0 2 3 1 3 4 2Example> diff(range(x1))Output[1] 4 Example> x2 x2Output[1] 4 5 3 10 2 4 2 4 8 7 3 1 5 6 7 3 7 3 4 5 3 7 2 7 ...
Read MoreHow to reduce a matrix in R to echelon form?
The echelon form of a matrix is the matrix that has the following characteristics:1. The first non-zero element in each row, called the leading entry, is 1.2. Each leading entry is in a column to the right of the leading entry in the previous row.3. Rows with all zero elements, if any, are below rows having a non-zero element.In R, we can use echelon function of matlib package to find the echelon form of the matrix.Example> M MOutput [, 1] [, 2] [, 3] [, 4] [, 5] [1, ] 8 11 3 10 13 [2, ] 9 ...
Read MoreHow to convert a string vector into an integer vector in R?
A string vector contains element inside double-quotes and an integer vector does not have any quotes. Sometimes integer values are stored in double-quotes hence the vector of these values is treated as a string vector in R but we need the integer values to perform mathematical operations. Therefore, we can use as.integer function to convert the string vector into an integer vector.Example1> x1 x1Output[1] "3" "2" "1" "2" "1" "1" "1" "1" "1" "1" "3" "3" "3" "1" "2" "1" "1" "2" [19] "2" "3" "3" "3" "3" "2" "3" "3" "3" "2" "1" "2" "3" "3" "2" "1" "2" "2" [37] "3" "3" "3" "2" "3" "2" "2" "1" "3" "3" "2" "2" "2" "1" "2" "3" "1" "3" [55] "3" "2" "1" "2" "2" "1" "2" "1" "1" "2" "2" "2" "3" "1" "3" "3" "1" "3" [73] "1" "1" "2" "2" "1" "3" "2" "3" "2" "2" "2" "2" "1" "3" "2" "1" "3" "3" [91] "3" "3" "1" "1" "1" "2" "2" "2" "2" "3" "1" "2" "2" "1" "3" "2" "2" "2" [109] "1" "1" "2" "3" "2" "2" "1" "1" "2" "2" "3" "2" "3" "2" "3" "2" "3" "2" [127] "2" "2" "2" "1" "1" "2" "1" "2" "2" "3" "3" "2" "2" "2" "3" "3" "2" "2" [145] "3" "2" "2" "3" "2" "3"Example> x1 x1Output[1] 3 2 1 2 1 1 1 1 1 1 3 3 3 1 2 1 1 2 2 3 3 3 3 2 3 3 3 2 1 2 3 3 2 1 2 2 3 [38] 3 3 2 3 2 2 1 3 3 2 2 2 1 2 3 1 3 3 2 1 2 2 1 2 1 1 2 2 2 3 1 3 3 1 3 1 1 [75] 2 2 1 3 2 3 2 2 2 2 1 3 2 1 3 3 3 3 1 1 1 2 2 2 2 3 1 2 2 1 3 2 2 2 1 1 2 [112] 3 2 2 1 1 2 2 3 2 3 2 3 2 3 2 2 2 2 1 1 2 1 2 2 3 3 2 2 2 3 3 2 2 3 2 2 3 [149] 2 3Example2> x2 x2Output[1] "19" "1" "19" "1" "13" "7" "11" "1" "13" "3" "19" "7" "3" "11" "7" [16] "3" "5" "1" "3" "11" "3" "2" "3" "5" "7" "7" "19" "7" "11" "7" [31] "7" "5" "17" "11" "7" "17" "2" "5" "5" "5" "5" "1" "13" "13" "5" [46] "19" "1" "13" "3" "3" "3" "19" "7" "7" "2" "3" "5" "1" "2" "5" [61] "3" "17" "11" "1" "13" "1" "1" "19" "17" "2" "17" "17" "11" "17" "13" [76] "2" "5" "2" "1" "17" "5" "5" "1" "13" "2" "13" "2" "2" "13" "19" [91] "3" "2" "1" "2" "11" "11" "13" "17" "19" "11" "19" "11" "1" "5" "19" [106] "7" "13" "19" "13" "11" "17" "11" "19" "2" "7" "19" "5" "17" "17" "5" [121] "1" "1" "7" "5" "11" "5" "7" "17" "13" "5" "1" "17" "13" "3" "1" [136] "17" "5" "5" "1" "2" "19" "11" "11" "7" "1" "5" "7" "13" "3" "2" [151] "2" "5" "17" "2" "7" "19" "19" "19" "7" "3"Example> x2 x2Output[1] 19 1 19 1 13 7 11 1 13 3 19 7 3 11 7 3 5 1 3 11 3 2 3 5 7 [26] 7 19 7 11 7 7 5 17 11 7 17 2 5 5 5 5 1 13 13 5 19 1 13 3 3 [51] 3 19 7 7 2 3 5 1 2 5 3 17 11 1 13 1 1 19 17 2 17 17 11 17 13 [76] 2 5 2 1 17 5 5 1 13 2 13 2 2 13 19 3 2 1 2 11 11 13 17 19 11 [101] 19 11 1 5 19 7 13 19 13 11 17 11 19 2 7 19 5 17 17 5 1 1 7 5 11 [126] 5 7 17 13 5 1 17 13 3 1 17 5 5 1 2 19 11 11 7 1 5 7 13 3 2 [151] 2 5 17 2 7 19 19 19 7 3Example3> x3 x3Output[1] "5" "10" "20" "15" "5" "15" "5" "5" "20" "20" "10" "10" "5" "5" "5" [16] "15" "5" "5" "15" "10" "10" "20" "20" "10" "20" "10" "5" "5" "15" "15" [31] "15" "15" "5" "10" "20" "15" "20" "5" "15" "20" "5" "20" "5" "20" "20" [46] "15" "15" "20" "5" "5" "10" "15" "15" "20" "20" "5" "5" "15" "20" "20" [61] "10" "10" "15" "10" "20" "5" "5" "15" "20" "5" "20" "20" "20" "5" "20" [76] "20" "15" "15" "15" "20" "10" "10" "15" "10" "10" "5" "5" "20" "20" "5" [91] "5" "10" "15" "15" "15" "10" "15" "20" "10" "20" "5" "10" "10" "15" "15" [106] "5" "15" "15" "10" "10" "20" "5" "20" "15" "10" "15" "15" "20" "20" "15" [121] "20" "20" "5" "5" "5" "5" "10" "20" "20" "10" "20" "5" "5" "20" "10" [136] "5" "5" "15" "10" "15" "10" "20" "20" "10" "20" "10" "20" "10" "15" "5" [151] "20" "20" "20" "15" "10" "20" "20" "10" "20" "20"Example> x3 x3Output[1] 5 10 20 15 5 15 5 5 20 20 10 10 5 5 5 15 5 5 15 10 10 20 20 10 20 [26] 10 5 5 15 15 15 15 5 10 20 15 20 5 15 20 5 20 5 20 20 15 15 20 5 5 [51] 10 15 15 20 20 5 5 15 20 20 10 10 15 10 20 5 5 15 20 5 20 20 20 5 20 [76] 20 15 15 15 20 10 10 15 10 10 5 5 20 20 5 5 10 15 15 15 10 15 20 10 20 [101] 5 10 10 15 15 5 15 15 10 10 20 5 20 15 10 15 15 20 20 15 20 20 5 5 5 [126] 5 10 20 20 10 20 5 5 20 10 5 5 15 10 15 10 20 20 10 20 10 20 10 15 5 [151] 20 20 20 15 10 20 20 10 20 20Example4> x4 x4Output[1] "501" "515" "515" "501" "515" "525" "501" "515" "515" "520" "525" "520" [13] "515" "501" "501" "525" "520" "515" "525" "525" "525" "525" "515" "515" [25] "515" "525" "520" "520" "525" "501" "520" "525" "520" "520" "501" "515" [37] "525" "520" "501" "501" "515" "520" "515" "520" "520" "520" "515" "501" [49] "515" "501" "520" "501" "525" "501" "501" "501" "525" "520" "520" "525" [61] "520" "501" "525" "520" "515" "520" "520" "525" "515" "515" "520" "520" [73] "520" "515" "515" "501" "525" "525" "501" "515" "525" "520" "515" "520" [85] "525" "525" "501" "501" "525" "515" "501" "525" "520" "501" "501" "501" [97] "501" "525" "501" "520" "520" "515" "501" "515" "515" "501" "520" "501" [109] "525" "525" "520" "515" "501" "520" "520" "515" "515" "501" "501" "520" [121] "515" "525" "501" "515" "501" "515" "515" "501" "520" "515" "501" "520" [133] "515" "520" "520" "515" "525" "515" "525" "515" "525" "520" "520" "515" [145] "515" "520" "501" "515" "525" "520"Example> x4 x4Output[1] 501 515 515 501 515 525 501 515 515 520 525 520 515 501 501 525 520 515 [19] 525 525 525 525 515 515 515 525 520 520 525 501 520 525 520 520 501 515 [37] 525 520 501 501 515 520 515 520 520 520 515 501 515 501 520 501 525 501 [55] 501 501 525 520 520 525 520 501 525 520 515 520 520 525 515 515 520 520 [73] 520 515 515 501 525 525 501 515 525 520 515 520 525 525 501 501 525 515 [91] 501 525 520 501 501 501 501 525 501 520 520 515 501 515 515 501 520 501 [109] 525 525 520 515 501 520 520 515 515 501 501 520 515 525 501 515 501 515 [127] 515 501 520 515 501 520 515 520 520 515 525 515 525 515 525 520 520 515 [145] 515 520 501 515 525 520
Read MoreHow to find the sum of diagonal elements in a table in R?
The sum of diagonal elements could be required in matrix analysis therefore, we can convert the matrix into a table and find the sum of diagonal elements. This can be easily done by using sun function by extracting diagonal elements of the table using diag function. For example, if we have a table T then the sum of diagonal elements of T can be found as sum(diag(T)).ExampleTable1
Read MoreHow to find the intersection between two or more lists in R?
The intersection of lists means the elements that are unique and common between the lists. For example, if we have a list that contains 1, 2, 3, 3, 3, 2, 1 and the other list that contains 2, 2, 1, 2, 1 then the intersection will return only those elements that are common between the lists and also unique, hence for this example we will get 1 and 2. In R, we can do this by using intersection function along with Reduce function.Consider the below lists −ExampleList1
Read MoreHow to exclude extra margin between points and the axes for a plot created by using ggplot2 in R?
In a plot created by using ggplot package there exists an extra area around all the sides of the plot which uses extra space, thus we might want to get rid of that space by removing that extra margin area. It can be done by setting the scale for both the axes to zero with the help of scale_x_continuous and scale_y_continuous function.Consider the below data frame −Exampleset.seed(151) x
Read MoreProgram to find maximum profit by cutting the rod of different length in C++
Suppose we have a rod is given of length n. We also have a list, that contains different size and price for each size. We have to find the maximum price by cutting the rod and selling them in the market. To get the best price by making a cut at different positions and comparing the prices after cutting the rod.So, if the input is like prices = [1, 5, 8, 9, 10, 17, 17, 20], n = 8, then the output will be 22, as by cutting the rod in length 2 and 6. The profit is 5 + ...
Read MoreProgram to reverse a sentence words stored as character array in C++
Suppose we have one input string sentence where each element is stored as single character, we have to reverse the strings word by word.So, if the input is like ["t", "h", "e", " ", "m", "a", "n", " ", "i", "s", " ", "n", "l", "c", "e"], then the output will be ["n", "l", "c", "e", " ", "i", "s", " ", "m", "a", "n", " ", "t", "h", "e"]To solve this, we will follow these steps −reverse the array sj := 0n := size of sfor initialize i := 0, when i < n, update (increase i by 1), ...
Read MoreProgram to multiply two strings and return result as string in C++
Suppose we have two numbers as string. We have to multiply them and return the result also in string. So if the numbers are “28” and “25”, then the result will be “700”To solve this, we will follow these steps −Taking two arguments x and y it indicates x divides yif x < −Infinity and y = 1, then return infinitya := |x|, b := |y| and ans := 0while a − b >= 0p := 0while a − (left shifted b (left shifted 1 p times)) >= 0p := p + 1a := a − (left shift b, p ...
Read More