How to sort matrix columns independently in increasing order in R?

Nizamuddin Siddiqui
Updated on 11-Mar-2026 22:50:55

554 Views

To sort a matrix column independently in increasing order means that ordering each column of the data frame in increasing order, therefore, sorting of values in one column does not affect the sorting in other columns. This can be done with the help of apply function along with the sort function as shown in the below examples.ExampleM1

How to save an xtable file locally using R?

Nizamuddin Siddiqui
Updated on 11-Mar-2026 22:50:55

792 Views

To save an xtable file locally, obviously we first need to create the xtable and then use the print function for saving the file. Therefore, we require xtable package loaded in R environment and the data set that we want to save as an xtable file. In the below example, we have used iris data in base R for this purpose. Take a look at the example to understand how it works.Loading xtable package −library(xtable)Considering iris data −Exampledata(iris) head(iris, 20)OutputSepal.Length Sepal.Width Petal.Length Petal.Width   Species 1  5.1           3.5         1.4     ... Read More

How to find the two factor interaction variables in an R data frame?

Nizamuddin Siddiqui
Updated on 11-Mar-2026 22:50:55

371 Views

If we have a data frame called df that contains four columns say x, y, z, and a then the two factor interaction columns will be xy, xz, xa, yz, ya, za. To find how many two factor interaction variables can be created using data frame columns, we can make use of combn function as shown in the below examples.Consider the below data frame −Examplex1

Find a value whose XOR with given number is maximum in C++

Hafeezul Kareem
Updated on 11-Mar-2026 22:50:55

310 Views

In this tutorial, we are going to write a program that finds the number whose XOR operation with the given number is maximum.We are assuming the number of bits here is 8.The XOR operation of different bits gives you the 1 bit. And the XOR operation between the same bits gives you the 0 bit.If we find the 1's complement of the given number, then that's the number we are looking for.ExampleLet's see the code.#include using namespace std; int findNumberWithMaximumXOR(int X) {    return ((1

How to display a line in segment of a plot using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 11-Mar-2026 22:50:55

417 Views

To display a line in segment of a plot, we can use geom_segment function of ggplot2 package where we need to pass the initial and the ending values for both the axes. For example, if we have data frame called df that contains x and y then a scatterplot with a line segment can be created by using the below command −ggplot(df,aes(x,y))+geom_point()+ geom_segment(aes(x=xstart,xend=xlast,y=ystart,yend=ylast))Consider the below data frame −Examplex

How to extract only factor columns name from an R data frame?

Nizamuddin Siddiqui
Updated on 11-Mar-2026 22:50:55

2K+ Views

To extract only factor column names from an R data frame, we can use names function with Filter by selecting only factor columns using as.factor. For example, if we have a data frame called df that contains some factor columns then the extraction of names of these factor columns can be done by using names(Filter(is.factor,df)).Consider the below data frame −Examplex1

Find all triplets with zero sum in C++

Hafeezul Kareem
Updated on 11-Mar-2026 22:50:55

517 Views

In this tutorial, we are going to write a program that finds the triplet in the array whose sum is equal to the given number.Let's see the steps to solve the problem.Create the array with dummy data.Write three inner loops for three elements that iterate until the end of the array.Add the three elements.Compare the sum with 0.If both are equal, then print the elements and break the loops.ExampleLet's see the code.#include using namespace std; void findTripletsWithSumZero(int arr[], int n){    bool is_found = false;    for (int i = 0; i < n-2; i++) {       for ... Read More

How to plot the regression line starting from origin using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 11-Mar-2026 22:50:55

1K+ Views

The regression line starting from origin means that the intercept of the model is dropped from the regression model. To plot the regression line starting from origin, we can use the formula by subtracting 1 in geom_smooth function of ggplot2 package.Consider the below data frame −Examplex

Find all divisors of a natural number - Set 1 in C++

Hafeezul Kareem
Updated on 11-Mar-2026 22:50:55

6K+ Views

In this tutorial, we are going to write a program that finds all the divisors of a natural number. It's a straightforward problem. Let's see the steps to solve it.Initialize the number.Write a loop that iterates from 1 to the given number.Check whether the given number is divisible by the current number or not.If the above condition satisfies, then print the current number.ExampleLet's see the code.#include using namespace std; void findDivisors(int n) {    for (int i = 1; i

Find all divisors of a natural number - Set 2 in C++

Hafeezul Kareem
Updated on 11-Mar-2026 22:50:55

3K+ Views

In this tutorial, we are going to write a program that finds all the divisors of a natural number. It's a straightforward problem. Let's see the steps to solve it.Initialize the number.Write a loop that iterates from 1 to the square root of the given number.Check whether the given number is divisible by the current number or not.If the above condition satisfies, then print the current number and given_number/current_number.ExampleLet's see the code.#include using namespace std; void findDivisors(int n) {    for (int i = 1; i

Advertisements