Server Side Programming Articles

Page 1164 of 2109

How to truncate a numerical vector to a specified number of decimal places in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 4K+ Views

The truncation means removing the number of decimal places but not rounding. For example, if we have a value 5.1742145 then truncating to one decimal place will be 5.1 and rounding will be 5.2. In R, we can do this by using trunc function as shown in the below examples.Examplex1

Read More

How to replace upper triangular matrix with lower triangular matrix and vice versa in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 1K+ Views

The upper triangular matrix can be replaced with lower triangular matrix by transposing the whole matrix and extracting upper triangular matrix from it then storing it in the original matrix. For example, if we have a matrix M then upper triangular matrix of M can be replaced with lower triangular matrix by using the below code −M1[upper.tri(M1)]

Read More

How to find the sum of division in R if zero exists in the vectors?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 303 Views

To find the sum of division if zero exists in the vectors, we need to assign NA to zeros in both the vectors and then use the sum function with na.rm set to TRUE. For example, if we have two vectors x and y that contains some zeros then we can divide x by y using the below commands −x[x==0] y yOutput[1] 1 5 3 1 9 1 3 8 9 0 1 7 3 2 3 3 2 9 3 1 9 5 5 2 5 4 4 7 4 5 9 1 9 9 4 2 3 [38] ...

Read More

How to check if all values in a vector are integer or not in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 3K+ Views

To check whether all values in a vector in R are integer or not, we can round the vector using floor function then subtract the vector values from it and check whether the output is zero or not. If the output will be zero that means the value is integer otherwise it is not. The floor function returns the largest integer that is smaller or equal to the actual value. For example, if we have a vector x then it can be done as x-floor(x)==0.Example1> x1 x1Output[1] 4 0 2 8 6 1 3 7 3 4 0 7 2 ...

Read More

How to set a specific value for a range in an R vector?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 409 Views

Suppose we have a vector that contains hundred values starting from 1 to 100 and we want to set values greater than 5 and less than 96 to 5 then it can be done with the help of ifelse function. For example, if such vector is named as x then the command will be as follows −ifelse(x>5 & x x1 x1Output[1] 2 4 1 6 7 4 0 1 6 4 0 7 1 3 3 1 4 6 7 7 0 2 7 3 9 4 4 8 6 3 3 5 4 5 6 5 6 [38] 2 ...

Read More

How to convert a string vector into title case in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 753 Views

We cannot be sure about the data characteristics we get for analysis and mostly it is not well organised, thus, the first task would be to make it more organised. The string values not in title case should also be taken care of if it is especially supposed to be in title case. For this purpose, we can str_to_title function of stringr package.Example1> x1 x1Output[1] "india" "united kingdom" "indonesia" "canada" [5] "canada" "india" "united kingdom" "canada" [9] "indonesia" "united kingdom" "indonesia" "canada" [13] "russia" "indonesia" "canada" "russia" [17] "united kingdom" "russia" "russia" "india" [21] "united kingdom" "india" "india" "united kingdom" ...

Read More

Deletion in a Binary Tree in C++?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 405 Views

The deletion is to be performed by replacing the deleted mode by bottom and rightmost node.Let us first define the struct that would represent a tree node that contains the data and its left and right node child. If this is the first node to be created then it’s a root node otherwise a child node.struct Node {    int data;    struct Node *leftChild, *rightChild; };Next we create our newNode(int data) function that takes an int value and assign it to the data member of the node. The function returns the pointer to the created struct Node. Also, the ...

Read More

Euler's Four Square Identity in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 309 Views

In this problem, we are given two numbers and we need to find the product of the numbers using Euler's four square Identity. Euler’s Four Square Identity is the method to find the product of two numbers which can be represented using the sum of four squares of the number if the numbers can be represented as sum of four squares.The product a * b can be represented as the sum of four squares ifa = x12 + x22 + x32 + x42 b = y12 + y22 + y32 + y42 a * b = z12 + z22 + z32 + z42Let’s take an example ...

Read More

Count pairs in an array that hold i*arr[i] > j*arr[j] in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 276 Views

We are given an array of numbers. The goal is to find the pair of elements of array such that they hold the conditionIf (i*arr[i] > j*arr[j]) then (arr[i], arr[j]) is a valid pair.If the array is [ 5, 4, 3, 2, 1 ] then pairs will be [3, 1] and [2, 1].Let us understand with examples.Input − arr[] = [ 1, 5, 4, 1, 2, 8, 3 ]Output − Count of pairs in an array that hold i*arr[i] > j*arr[j] are − 3Explanation − Pairs are (5, 1), (4, 1), (8, 3)Input − arr[] = [ -1, -2, 3, ...

Read More

Count of sub-strings of length n possible from the given string in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 758 Views

We are given a string str[] and a number n. The goal is to find all substrings of str[] that have length n. If string is “abcde” and n=3 then substrings of length 3 are “abc”, “bcd”, “cde” and count is 3.Let us understand with examples.Input − str[] = “computer” n=4Output − Count of substrings of length n possible from the given string are − 5Explanation − Substrings with length 4 are: “comp”, “ompu”, ”mput”, “pute”, “uter”Input − str[] = “development” n=5Output − Count of substrings of length n possible from the given string are − 7Explanation − Substrings with ...

Read More
Showing 11631–11640 of 21,090 articles
Advertisements